offer到底长什么样:地平线薪资感人
0 点赞 评论 收藏
分享
yjk89446746:#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int x,y;
vector<vector<vector<int>>> dp;
char k[4] = {'1','2','3','4'};
char d[4] = {'F','Z','Y','G'};
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int res;
int main(int argc, const char * argv[]) {
// insert code here...
int x_,y_,n_;
while (cin>>n) {
cin>>m;
cin>>x>>y;
vector<vector<char>> gp(n,vector<char>(m));
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin>>gp[i][j];
}
}
dp = vector<vector<vector<int>>>(n,vector<vector<int>>(m,vector<int>((1<<9)-1,0x3f3f3f3f)));
dp[x][y][0] = 0;
res = 0x3f3f3f3f;
vector<int> tmp;
queue<vector<int>> q;
q.push({x,y,0});
while(!q.empty())
{
int k = q.size();
set<vector<int>> vis;
while(k--)
{
tmp = q.front();
q.pop();
int val = dp[tmp[0]][tmp[1]][tmp[2]];
for(int i=0;i<4;i++)
{
x_ = tmp[0]+dx[i];
y_ = tmp[1]+dy[i];
if(0>x_||0>y_||n<=x_||m<=y_||gp[x_][y_]=='#')
continue;
n_ = tmp[2];
if(gp[x_][y_]=='1')
n_ = tmp[2]|1;
else if(gp[x_][y_]=='2')
n_ = tmp[2]|2;
else if(gp[x_][y_]=='3')
n_ = tmp[2]|4;
else if(gp[x_][y_]=='4')
n_ = tmp[2]|8;
else if(gp[x_][y_]=='F'&&n_&1)
n_ = tmp[2]|16;
else if(gp[x_][y_]=='Z'&&n_&2)
n_ = tmp[2]|32;
else if(gp[x_][y_]=='Y'&&n_&4)
n_ = tmp[2]|64;
else if(gp[x_][y_]=='G'&&n_&8)
n_ = tmp[2]|128;
if(val+1<dp[x_][y_][n_])
{
dp[x_][y_][n_] = val+1;
vis.insert({x_,y_,n_});
if(n_==(1<<8)-1)
res = min(res,val+1);
}
}
}
for (auto v:vis) {
q.push(v);
}
}
if (res==0x3f3f3f3f) {
res = -1;
}
cout<<res<<endl;
}
return 0;
} 大概是这样吧,中间接了个电面,这题写完没发现dp数组开小了,没debug就交了 (1<<8)-1 改成 (1<<9)-1 这样应该没问题了 第四题没写...mmp气死我了
投递小马智行等公司10个岗位 >
0 点赞 评论 收藏
分享
关注他的用户也关注了: