- 走迷宫
涨知识了
- 2025-6-29 13:35:10 @
原代码:
#include<iostream>
#include<queue>
using namespace std;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
struct node{
int x,y,cnt;
};
queue<node> q;
void bfs(int i,int j){
q.push({i,j,1});
vis[i][j]=0;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==r&&t.y==c){
cout<<t.cnt;
break;
}
for(int ppp=0;ppp<4;ppp++){
int nx=t.x+dx[ppp],ny=t.y+dy[ppp];
if(nx>0&&ny>0&&nx<=r&&ny<=c&&vis[nx][ny]){
q.push({nx,ny,t.cnt+1});
vis[nx][ny]=0;
}
}
}
}
int r,c;
bool vis[114][514];
int main(){
cin>>r>>c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
char t;
cin>>t;
vis[i][j]=(c=='.'?1:0);
}
}
bfs(1,1);
return 0;
}
出现了compile error
为什么呢?
上网上查了之后才知道是因为r,c和vis是在bfs后定义的,然后编译器就寺掉了
这个故事告诉我们要尽量把变量定义在代码的前面
0 comments
No comments so far...
Information
- ID
- 737
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 139
- Accepted
- 49
- Uploaded By