BFS入门导引二
include
include
include
using namespace std;
const int maxn = 100;
struct node{
int x,y;
int step;
}S,T,Node;
int n,m;//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
q.push(S);
while(!q.empty()){
node top = q.front();//取出队首元素
q.pop();
if(top.xT.x&&top.yT.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的坐标为(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
for(int j=0;j
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());
return 0;
}