解题思路:
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:967 |
C语言程序设计教程(第三版)课后习题9.1 (Java代码)浏览:471 |
回文数(一) (C语言代码)浏览:728 |
妹子杀手的故事 (C语言代码)浏览:1218 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:850 |
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:575 |
三角形 (C语言代码)浏览:903 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:504 |
DNA (C语言代码)浏览:735 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1460 |