1 条题解
-
0
#include<bits/stdc++.h> using namespace std; int n,sx,sy,ex,ey; char mp[1001][1001]; bool vs[1001][1001]; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; struct Node{ int x,y,step; }; int bfs(int sx,int sy){ queue q; Node t={sx,sy,1}; q.push(t); vs[sx][sy]=1; // 开始搜索 while(!q.empty()){ t=q.front(); q.pop(); //到终点 if(t.xex && t.yey){ return t.step; } for(int i=0;i<4;i++){ int xx=t.x+dx[i]; int yy=t.y+dy[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&mp[xx][yy]!='1'&&vs[xx][yy]!=1){ q.push({xx,yy,t.step+1}); vs[xx][yy]=1; } } } return -1; } int main(){ cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>mp[i][j]; } } cin>>sx>>sy>>ex>>ey; cout<<bfs(sx,sy)-1; return 0; }
- 1
信息
- ID
- 544
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 12
- 已通过
- 4
- 上传者