G-POJ-3984 迷宫问题

简介: POJ-3984 Time Limit: 1000MS Memory Limit: 65536KDescription 定义一个二维数组: in...

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;
}
目录
相关文章
|
7月前
Poj 3255(dijkstra求次短路)
Poj 3255(dijkstra求次短路)
43 0
洛谷 P1141 01迷宫
洛谷 P1141 01迷宫
84 0
洛谷P1605:迷宫
洛谷P1605:迷宫
88 0
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
|
人工智能 并行计算 网络架构
|
人工智能
POJ 2370 Democracy in danger(简单贪心)
Democracy in danger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3388   Accepted: 2508 Description In one of the...
957 0
poj supermaket (贪心)
http://poj.org/problem?id=1456 #include #include #include using namespace std; struct nod { int a; int d; }; bool cmp(nod x,nod y) { return x.
699 0