解题思路:
注意事项:
参考代码:
#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语言代码)浏览:633 |
用筛法求之N内的素数。 (C语言代码)浏览:669 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:1172 |
罗列完美数 (C语言代码)浏览:491 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:509 |
川哥的吩咐 (C语言代码)浏览:611 |
C语言程序设计教程(第三版)课后习题12.6 (C语言代码)浏览:662 |
整除的尾数 (C语言代码)浏览:814 |
C语言训练-求矩阵的两对角线上的元素之和 (C语言代码)浏览:939 |
WU-C语言程序设计教程(第三版)课后习题12.5 (C++代码)浏览:977 |