解题思路:
注意事项:
参考代码:
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<bits/stdc++.h> using namespace std; struct Node { int x; int y; int steps; }; int n,m; char a[101][101]; bool bj[101][101]={0}; queue<Node> q; int flag=0; int xm[5]={0,1,-1,0,0}; int ym[5]={0,0,0,1,-1}; void bfs() { Node start; start.x=1; start.y=1; start.steps=1; q.push(start); bj[1][1]=1; while(!q.empty()) { if(q.front().x==n&&q.front().y==m) {return;} else{ for(int i=1;i<=4;i++) { int tx=q.front().x+xm[i]; int ty=q.front().y+ym[i]; if(a[tx][ty]=='.'&&bj[tx][ty]==0) { Node newnode; newnode.x=tx; newnode.y=ty; bj[tx][ty]=1; newnode.steps=q.front().steps+1; q.push(newnode); } } q.pop(); } } } int main() { cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>a[i][j]; } } bfs(); cout<<q.back().steps<<endl; return 0; }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:1074 |
亲和的解法浏览:1054 |
点我有惊喜!你懂得!浏览:1513 |
C二级辅导-求偶数和 (C语言代码)浏览:603 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:572 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:544 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:1066 |
简单编码 (C++代码)浏览:675 |
C语言训练-计算1~N之间所有奇数之和 (C语言代码)浏览:644 |
小明A+B (C语言代码)浏览:1246 |