社区讨论
不知道那里错了,求调
P2298Mzc和男家丁的游戏参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhjuwz1x
- 此快照首次捕获于
- 2025/11/04 08:54 4 个月前
- 此快照最后确认于
- 2025/11/04 08:54 4 个月前
本蒟蒻的代码
CPP#include <bits/stdc++.h>
using namespace std;
int start_x,start_y,end_x,end_y;
struct node{
int x,y,step;
};
int find_mzc[2050][2050];
int vis[2050][2050];
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
int n,m;
queue <node> q;
void bfs(int x,int y,int step){
node tmp;
tmp.x=x;
tmp.y=y;
tmp.step=step;
q.push(tmp);
while(!q.empty()){
node t=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=t.x+dx[i],yy=t.y+dy[i];
if (xx<1 || xx>n || yy<1 || yy>m) continue;
if (find_mzc[xx][yy] || vis[xx][yy]) continue;
if (xx==end_x && yy==end_y){
cout<<t.step+1;
return ;
}
t.x=xx;
t.y=yy;
t.step=t.step+1;
vis[xx][yy]=1;
q.push(t);
}
}
cout<<"No Way!";
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char x;
cin>>x;
if(x=='#') find_mzc[i][j]=1;
if(x=='m'){
start_x=i;
start_y=j;
}
if(x=='d'){
end_x=i;
end_y=j;
}
}
}
vis[start_x][start_y]=1;
bfs(start_x,start_y,0);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...