C_Boa @ 2024-04-18 12:27:17
#include<bits/stdc++.h>
using namespace std;
char a[105][105],vis[105][105];
int n,m,cnt=0,startx,starty,endx,endy;
struct node{
int x,y;
};
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
queue<node> q;
void bfs(int t,int u){
node b;
b.x=t;
b.y=u;
q.push(b);
vis[b.x][b.y]=1;
while(!q.empty()){
node c;
c=q.front();
q.pop();
for(int i=0;i<4;i++){
int dx=c.x+dir[i][0];
int dy=c.y+dir[i][1];
if(dx>0&&dx<=n&&dy>0&&dy<=n&&vis[dx][dy]==0&&a[dx][dy]=='.'){
vis[dx][dy]=1;
cnt++;
if(dx==endx&&dy==endy){
cout<<cnt<<endl;
return;
}
b.x=dx;
b.y=dy;
q.push(b);
}
}
}
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
cin>>startx>>starty>>endx>>endy;
bfs(1,1);
return 0;
}
by wangzhongqian @ 2024-05-08 20:40:54
代码给你改好了
#include<bits/stdc++.h>
using namespace std;
char a[1005][1005];
bool vis[1005][1005];
int n,m,startx,starty,endx,endy;
struct node{
int x,y,cnt;
}b,c;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
queue<node> q;
void bfs(int t,int u){
q.push({t,u,0});
vis[t][u]=1;
while(!q.empty()){
c=q.front();
q.pop();
for(int i=0;i<4;i++){
int dx=c.x+dir[i][0];
int dy=c.y+dir[i][1];
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&vis[dx][dy]==0&&a[dx][dy]=='0'){
vis[dx][dy]=1;
if(dx==endx&&dy==endy){
cout<<c.cnt+1<<endl;
return;
}
b.x=dx;
b.y=dy;
b.cnt=c.cnt+1;
q.push(b);
}
}
}
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
cin>>startx>>starty>>endx>>endy;
bfs(startx,starty);
return 0;
}
by wangzhongqian @ 2024-05-08 20:41:11
@C_Boa
by wangzhongqian @ 2024-05-08 20:41:42
求关(逃
by C_Boa @ 2024-05-10 12:03:56
@wangzhongqian OKOK 已关注