K - 迷宫问题
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, };
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
开始自己摸索着写出来一个自己认为非常正确的程序,后来感觉数据有点不对劲,直接输出题目中的数据发现ac了!!原来只有一组。。。换了几组自己编的数据,发现自己错了。经过我的大神舍友胖宝贝指导,给了我一个非常规范的bfs模板,以后自己的代码规范。。。
我的错误程序:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int map[5][5],dir[4][2]={1,0,-1,0,0,1,0,-1},p[5][5],c[10001][2],d[10001][2],m,n;
struct t{
int x;
int y;
int sum;
};
void bfs()
{
queue<t>a;
t b;
b.x=0;
b.y=0;
b.sum=0;
a.push(b);
p[0][0]=1;
while(!a.empty()){
t temp=a.front();
a.pop();
int xi=temp.x;
int yi=temp.y;
int sumi=temp.sum;
if(xi==4&&yi==4){
return;
}
for(int i=0;i<4;i++)
{
t end;
int x1=end.x=xi+dir[i][0];
int y1=end.y=yi+dir[i][1];
int sum1=end.sum=sumi+1;
if(x1>4||x1<0||y1>4||y1<0) continue;
if(map[x1][y1]==0&&p[x1][y1]==0){
a.push(end);
p[x1][y1]=1;
c[m][0]=x1;
c[m][1]=y1;
m++;
}
}
}
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>map[i][j];
memset(p,0,sizeof(p));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
m=0;
bfs();
int j=1;
c[m][0]=4;c[m][1]=4;
d[0][0]=4;d[0][1]=4;
n=1;
for(int i=m-1;i>=0;i--){
j=1;
while(j++){
if((c[i-j][0]==c[i][0]+1||c[i-j][0]==c[i][0]-1)&&c[i-j][1]==c[i][1])
{
d[n][0]=c[i-j][0];d[n][1]=c[i-j][1];
i=i-j+1;
j=1;
n++;
break;
}
else if((c[i-j][1]==c[i][1]+1||c[i-j][1]==c[i][1]-1)&&c[i-j][0]==c[i][0])
{
d[n][0]=c[i-j][0];d[n][1]=c[i-j][1];
i=i-j+1;
j=1;
n++;
break;
}
}
}
d[n][0]=0;
d[n][1]=0;
for(int i=n;i>=0;i--)
cout<<"("<<d[i][0]<<", "<<d[i][1]<<")"<<endl;
return 0;
}
胖宝贝的规范程序:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int _max = 10;
char mp[_max][_max];
bool vis[_max][_max];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node{
int x,y;
};
node step[_max][_max];
int n;
bool check(node n1){
if(n1.x < 1 || n1.x > n) return false;
if(n1.y < 1 || n1.y > n) return false;
if(!vis[n1.x][n1.y]) return false;
return true;
}
void bfs(int sx,int sy){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = sx;
now.y = sy;
step[now.x][now.y ].x = sx;
step[now.x][now.y ].y = sy;
vis[now.x][now.y] = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
if(!check(next)) continue;
vis[next.x][next.y] = 0;
step[next.x][next.y].x = now.x;
step[next.x][next.y].y = now.y;
if(next.x == 5 && next.y == 5) return ;
q.push(next);
}
}
}
void printf_ans(int x,int y){
if(x != step[x][y].x || y != step[x][y].y)
printf_ans(step[x][y].x,step[x][y].y);
cout<<"("<<x-1<<", "<<y-1<<")"<<endl;
}
int main(){
n = 5;
memset(vis,1,sizeof(vis));
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++){
cin>>mp[i][j];
if(mp[i][j] == '1')
vis[i][j] = 0;
}
bfs(1,1);
printf_ans(n,n);
return 0;
}