解题思路:
注意事项:
参考代码:
#include <iostream> #include <queue> using namespace std; struct node{ int x; int y; int step; node(int xx,int yy,int sstep){ x=xx; y=yy; step=sstep; } }; char s[101][101]; int sx,sy,fx,fy;//起点和终点的横纵坐标 bool vis[101][101]; int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; int n,m; bool in(int x,int y){ return x>=0&&x<n&&y>=0&&y<m; } int bfs(int x,int y){ queue<node> q; q.push(node(x,y,0)); while(!q.empty()){ node a=q.front(); q.pop(); int x=a.x,y=a.y; for(int i=0;i<4;i++){ int tx=x+dir[i][0]; int ty=y+dir[i][1]; int tstep=a.step+1; if(in(tx,ty)&&!vis[tx][ty]&&s[tx][ty]=='-'){ vis[tx][ty]=true; q.push(node(tx,ty,tstep)); } if(tx==fx&&ty==fy){ return a.step+1; } } } return -1; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>s[i][j]; if(s[i][j]=='S'){ sx=i,sy=j; }else if(s[i][j]=='E'){ fx=i,fy=j; } } } cout<<bfs(sx,sy)<<endl; } return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:716 |
奖学金 (C++代码)浏览:2008 |
简单的a+b (C语言代码)浏览:542 |
打水问题 (C语言代码)浏览:1072 |
输出正反三角形 (C语言代码)格式错误!!!浏览:1140 |
A+B for Input-Output Practice (II) (C语言代码)浏览:999 |
WU-格式化数据输出 (C++代码)浏览:1212 |
WU-陶陶摘苹果2 (C++代码)浏览:973 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:565 |
1118(求助_已解决)浏览:329 |