1. 程式人生 > 實用技巧 >BFS計算迷宮最短步數

BFS計算迷宮最短步數

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<queue>

using namespace std;
const int maxn=100;
struct node {
    int x,y;
    int step; 
}S,T, Node;
int n,m;
char maze[maxn][maxn];
bool inq[maxn][maxn]= {false};
int X[4]= {0,0,1,-1};
int Y[4]= {1,-1,0,0};
bool test(int
x,int y) { if(x>=n||x<0||y>=m||y<0)return false; if(maze[x][y]=='*')return false; if(inq[x][y]==true)return false; return true; } int BFS() { queue<node>q; q.push(S); while(!q.empty()) { node top=q.front(); q.pop(); if(top.x==T.x&&top.y==T.y) {
return top.step; } for(int i=0; i<4; i++) { int newx=top.x+X[i]; int newy=top.y+Y[i]; if(test(newx,newy)) { Node.x=newx; Node.y=newy; Node.step=top.step+1; q.push(Node); inq[newx][newy]
=true; } } } return -1; } int main() { scanf("%d %d",&n,&m); for(int i=0; i<n; i++) { getchar();//過濾每行後的換行 for(int j=0; j<m; j++) { maze[i][j]=getchar(); } maze[i][m+1]='\0'; } scanf("%d%d%d%d",&S.x,&S.y,&T.x,&T.y); S.step=0; printf("%d\n",BFS()); 5 5 ..... .*.*. .*S*. .***. ...T* 2 2 4 3 return 0; }