600多的一题,欢迎观看!
题目描述:
有一个 m 行 n 列的迷宫,|表示不通,*表示通行,每一步可以沿上下左右四个方向,
还有四个整数 a, b, c, d,要求从 a 行 b 列走到 c 行 d 列的最少步数。
样例输入:
3 4 1 1 2 4 **** *||* ****
样例输出:
4
提示:
m,n<=10
时间限制: 1000ms
空间限制: 256MB
来源: USACO
解析
广搜可以解决,上代码!
制做不易,点个赞
#include<bits/stdc++.h> //包括万能库
using namespace std; //使用默认的命名空间
int maps[1010][1010],f[1010][1010];
int u[5]= {0,0,0,1,-1}; //定义4个方向的增量
int w[5]= {0,1,-1,0,0};
int n,m,n1,n2,m1,m2;
int x,y,step;
string ch;
struct data {
int x,y,step;
} ;
queue<data> q;
int main() {
cin>>n>>m>>n1>>n2>>m1>>m2;
for(int i=1; i<=n; i++) {
cin>>ch;
for(int j=0; j<m; j++) {
if(ch[j]=='*') maps[i][j+1]=0;
else maps[i][j+1]=1;
}
}
q.push( data {n1,n2,0 });
f[ n1 ][ n2 ] = 1;
while( !q.empty() ) {
data head=q.front();
q.pop();
for(int i=1;i<=4;i++){
int x=head.x+u[i];
int y=head.y+w[i];
if(x<1||x>n||y<1||y>m||maps[x][y]||f[x][y])
continue;
q.push( data {x,y,head.step+1});
f[x][y] = 1;
if(x==m1&&y==m2){
cout<<head.step+1;
return 0;
}
}
}
return 0;
}
结束了!