G-POJ-3984 迷宫问题

POJ-3984

Description

定义一个二维数组:

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)

BFS、DFS都可以

1.BFS:用树存路径压入栈,一次AC看代码吧:

//AC:736K   0MS 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
struct map{
    int x,y;
    int parent;
    int step;
}path[1000],S;
bool vis[5][5];
void BFS(int s[5][5]){
    map now,next;
    memset(vis,false,sizeof(vis));
    now.x=0,now.y=0,now.step=0;
    path[0].x=0,path[0].y=0;
    int n=1,m=-1;
    vis[0][0]=true;
    queue<map>Q;
    stack<map>N;
    Q.push(now);
    while(!Q.empty()){
        now=Q.front();
        Q.pop();
        m++;
        if(now.x==4&&now.y==4){
            N.push(path[m]);
            for(;;){
                m=path[m].parent;
                N.push(path[m]);
                if(m==0)
                    break;
            }
            for(int i=0;i<=now.step;i++){
                S=N.top();
                N.pop();
                printf("(%d, %d)\n",S.x,S.y);
            }
            return;
        }
        for(int j=1;j<=4;j++){
            if(j==1){
                next.x=now.x;
                next.y=now.y+1;
                next.step=now.step+1;
                if(!vis[next.x][next.y]&&0<=next.x&&next.x<=4&&0<=next.y&&next.y<=4&&s[next.x][next.y]!=1){
                    Q.push(next);
                    path[n].x=next.x;
                    path[n].y=next.y;
                    path[n].parent=m;
                    n++;
                }
            }
            if(j==2){
                next.x=now.x+1;
                next.y=now.y;
                next.step=now.step+1;
                if(!vis[next.x][next.y]&&0<=next.x&&next.x<=4&&0<=next.y&&next.y<=4&&s[next.x][next.y]!=1){
                    Q.push(next);
                    path[n].x=next.x;
                    path[n].y=next.y;
                    path[n].parent=m;
                    n++;
                }
            }
            if(j==3){
                next.x=now.x-1;
                next.y=now.y;
                next.step=now.step+1;
                if(!vis[next.x][next.y]&&0<=next.x&&next.x<=4&&0<=next.y&&next.y<=4&&s[next.x][next.y]!=1){
                    Q.push(next);
                    path[n].x=next.x;
                    path[n].y=next.y;
                    path[n].parent=m;
                    n++;
                }
            }
            if(j==4){
                next.x=now.x;
                next.y=now.y-1;
                next.step=now.step+1;
                if(!vis[next.x][next.y]&&0<=next.x&&next.x<=4&&0<=next.y&&next.y<=4&&s[next.x][next.y]!=1){
                    Q.push(next);
                    path[n].x=next.x;
                    path[n].y=next.y;
                    path[n].parent=m;
                    n++;
                }
            }
        }
    }

}
int main(){
    int maze[5][5];
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            scanf("%d",&maze[i][j]);
    BFS(maze);
    return 0;
}

2.DFS,这个写的时候由于每种情况分开写bug一大堆:

//AC: 708K  0MS
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
bool vis[5][5];
struct{
    int x,y;
}path[1000];
int n=0;
void DFS(int s[5][5],int x,int y,int step){
    vis[path[step].x][path[step].y]=true;
    for(int i=1;i<=4;i++){
        if(i==1){
            path[step+1].x=x+1;
            path[step+1].y=y;
            if(0<=path[step+1].x&&path[step+1].x<=4&&0<=path[step+1].y&&path[step+1].y<=4&&s[path[step+1].x][path[step+1].y]!=1&&!vis[path[step+1].x][path[step+1].y]){
                if(path[step+1].x==4&&path[step+1].y==4){
                    n=step;
                }
                if(n)   return;
                else{
                    DFS(s,path[step+1].x,path[step+1].y,step+1);
                    if(n)   break;
                    vis[path[step+1].x][path[step+1].y]=false;
                }
            }
        }
        if(i==2){
            path[step+1].x=x;
            path[step+1].y=y+1;
            if(0<=path[step+1].x&&path[step+1].x<=4&&0<=path[step+1].y&&path[step+1].y<=4&&s[path[step+1].x][path[step+1].y]!=1&&!vis[path[step+1].x][path[step+1].y]){
                if(path[step+1].x==4&&path[step+1].y==4){
                    n=step+1;
                }
                if(n)   return;
                else{
                    DFS(s,path[step+1].x,path[step+1].y,step+1);
                    if(n)   break;
                    vis[path[step+1].x][path[step+1].y]=false;
                }
            }
        }
        if(i==3){
            path[step+1].x=x-1;
            path[step+1].y=y;
            if(0<=path[step+1].x&&path[step+1].x<=4&&0<=path[step+1].y&&path[step+1].y<=4&&s[path[step+1].x][path[step+1].y]!=1&&!vis[path[step+1].x][path[step+1].y]){
                if(path[step+1].x==4&&path[step+1].y==4){
                    n=step+1;
                }
                if(n)   return;
                else{
                    DFS(s,path[step+1].x,path[step+1].y,step+1);
                    if(n)   break;
                    vis[path[step+1].x][path[step+1].y]=false;
                }
            }
        }
        if(i==4){
            path[step+1].x=x;
            path[step+1].y=y-1;
            if(0<=path[step+1].x&&path[step+1].x<=4&&0<=path[step+1].y&&path[step+1].y<=4&&s[path[step+1].x][path[step+1].y]!=1&&!vis[path[step+1].x][path[step+1].y]){
                if(path[step+1].x==4&&path[step+1].y==4){
                    n=step+1;
                }
                if(n)   return;
                else{
                    DFS(s,path[step+1].x,path[step+1].y,step+1);
                    if(n)   break;
                    vis[path[step+1].x][path[step+1].y]=false;
                }
            }
        }
    }
}
int main(){
    int maze[5][5];
    memset(vis,false,sizeof(vis));
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            scanf("%d",&maze[i][j]);
    DFS(maze,0,0,0);
    for(int i=0;i<=n+1;i++)
        printf("(%d, %d)\n",path[i].x,path[i].y);
    return 0;
}
上一篇:返回固定数据的web服务器


下一篇:ubuntu11.04服务器安装