G

106严梁羽 - 周赛7.A - 暴走迷宫

public
Guest Apr 23, 2025 Never 10
Clone
C++ 106严梁羽 - 周赛7.A - 暴走迷宫 72 lines (70 loc) | 1.62 KB
1
/*
2
hack 数据
3
3 3
4
001
5
000
6
000
7
1 1 3 3
8
正确 2
9
输出 3
10
*/
11
12
#include <bits/stdc++.h>
13
using namespace std;
14
long long n,m,xx,yy,x2,y2,vis[505][505];
15
char s[505][505];
16
int nx[]={0,0,1,-1},ny[]={1,-1,0,0};
17
struct node{
18
int x,y,i;
19
};
20
bool check(long long x,long long y){
21
// return x>0&&x<=n&&y>0&&y<=m&&s[x][y]=='0'&&vis[x][y]==0;
22
return x>0&&x<=n&&y>0&&y<=m&&s[x][y]=='0'; //访问过的点,可以经过
23
/*
24
3 3
25
001
26
000
27
000
28
1 1 3 3
29
从 (1,1) 出发
30
程序先通过 (1,2) 访问 (3,2) 步数为 2
31
再通过 (1,3) 到达 (3,3) 步数为 2 因为需要经过 (3,2),无法到达
32
33
故输出 为 3。 路线 (1,1) --> (1,2) --> (3,2) --> (3,3)
34
正确答案 2。
35
*/
36
}
37
int bfs(int stx,int sty){
38
queue<node> q;
39
if(stx==x2 && sty==y2) return 0; // 入口 就是 出口 ,虽然不特判也能 AC
40
q.push((node){stx,sty,0});
41
vis[stx][sty]=1;
42
while(!q.empty()){
43
node top=q.front();
44
q.pop();
45
for(int i=0;i<4;i++){
46
int dx=top.x ,dy=top.y ;
47
// if(vis[dx][dy]==1) continue;
48
// if(!check(dx,dy)) continue;
49
while(check(dx+nx[i],dy+ny[i])){
50
dx+=nx[i],dy+=ny[i];
51
if(dx==x2&&dy==y2) return top.i +1;
52
}
53
if(vis[dx][dy]==1) continue;
54
vis[dx][dy]=1;
55
q.push((node){dx,dy,top.i +1});
56
// if(dx==x2&&dy==y2) return top.i +1;
57
}
58
}
59
return -1;
60
}
61
int main(){
62
memset(vis,0,sizeof(vis));
63
cin>>n>>m;
64
// for(int i=0;i<=m;i++){
65
// s[i][0]=s[i][1+n]=1;
66
// }
67
for(int i=1;i<=n;i++) cin>>s[i]+1;
68
cin>>xx>>yy>>x2>>y2;
69
int ans=bfs(xx,yy);
70
if(ans==-1) cout<<"No!God!Please no!";
71
else cout<<ans;
72
}