poj3984 迷宫问题(简单搜索+记录路径)

来源:互联网 发布:微信淘宝客怎么拉人 编辑:程序博客网 时间:2024/05/28 15:46

定义一个二维数组:

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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个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 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
本来要做poj3414,发现记录路径不会,就先做了这道题。
投机取巧法(第一个代码)
自己想的方法(第二个代码),比较笨,明天学个简单的(第三个代码)

//不想说啥,不要用#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;int main(){    cout<<"(0, 0)"<<endl;    cout<<"(1, 0)"<<endl;    cout<<"(2, 0)"<<endl;    cout<<"(2, 1)"<<endl;    cout<<"(2, 2)"<<endl;    cout<<"(2, 3)"<<endl;    cout<<"(2, 4)"<<endl;    cout<<"(3, 4)"<<endl;    cout<<"(4, 4)"<<endl;    return 0;}
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<iostream>#include<algorithm>using namespace std;int mapp[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,};int go[4][2]= {0,1,0,-1,1,0,-1,0};int vist[5][5];struct node{    int xx;//记录x的坐标    int yy;//记录y的坐标    int lenn;//记录这个点的上一个点};int xxx[50],yyy[50];//记录x和y的坐标int next[50];//记录路径//为什么两个东西记录路径,一个是结构体,用来在图上走路的,所以会舍弃//另一个是数组,用来记录并输出void huisu(int i){    if(i==0)        return ;//没到起点就一直向前找    else    {        huisu(next[i]);//next[i]记录的是上一个点的位置        cout<<"("<<xxx[i]<<","<<" "<<yyy[i]<<")"<<endl;//找到起点后就一步一步输出    }}int dfs(int x,int y){    int len=0;//表示位置    xxx[len]=x,yyy[len]=y;    node now,nex;    queue<node>q;    now.xx=x,now.yy=y,now.lenn=len++;    q.push(now);    while(!q.empty())    {        now=q.front();        q.pop();        if(now.xx==4&&now.yy==4)            huisu(now.lenn);        for(int i=0; i<4; i++)        {            int X=now.xx+go[i][0],Y=now.yy+go[i][1];            if(X>=0&&X<5&&Y>=0&&Y<5&&!vist[X][Y]&&mapp[X][Y]==0)            {                vist[X][Y]=1;                xxx[len]=X;                yyy[len]=Y;                next[len]=now.lenn;//记录                nex.xx=X;                nex.yy=Y;                nex.lenn=len++;//记录上一个点的位置                q.push(nex);            }        }    }}int main(){    memset(vist,0,sizeof(vist));    memset(next,0,sizeof(next));    memset(xxx,0,sizeof(xxx));    memset(yyy,0,sizeof(yyy));    cout<<"(0, 0)"<<endl;    dfs(0,0);    return 0;}
//正规做法#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<algorithm>using namespace std;int mapp[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,};int go[4][2]= {0,1,0,-1,1,0,-1,0};int vist[5][5];struct node{    int xx;    int yy;    int pre;} num[50];void huisu(int i){    if(num[i].pre==-1)        return ;    else    {        huisu(num[i].pre);        cout<<"("<<num[i].xx<<", "<<num[i].yy<<")"<<endl;    }}int dfs(){    vist[0][0]=1;    int f,n;    f=n=1;    num[f].pre=-1;    num[f].xx=0;    num[f].yy=0;    while(f<=n)    {        if(num[f].xx==4&&num[f].yy==4)            huisu(num[f].pre);        for(int i=0; i<4; i++)        {            int x=num[f].xx+go[i][0],y=num[f].yy+go[i][1];            if(y>=0&&y<5&&x>=0&&x<5&&!vist[x][y]&&mapp[x][y]==0)            {                n++;                num[n].xx=x;                num[n].yy=y;                num[n].pre=f;//记录上一个点的位置                vist[x][y]=1;            }        }        f++;    }}int main(){    memset(vist,0,sizeof(vist));    cout<<"(0, 0)"<<endl;    dfs();    cout<<"(4, 4)"<<endl;    return 0;}
原创粉丝点击