zufeoj_迷宫-栈

题目链接:http://acm.ocrosoft.com/problem.php?cid=1172&pid=51


题目描述

给定一个M×N的迷宫图,求一条从指定入口到出口的路径。假设迷宫图如图所示(M=10,N=10),其中的方块图表示迷宫。对于图中的每个方块,用空白表示通道,用阴影表示墙。要求所求路径必须是简单路径,即在求得的路径上不能重复出现同一通道块。



 int a[][10]=

    {

        {1,1,1,1,1,1,1,1,1,1},

{1,0,0,1,0,0,0,1,0,1},

{1,0,0,1,0,0,0,1,0,1},

{1,0,0,0,0,1,1,0,0,1},

{1,0,1,1,1,0,0,0,0,1},

{1,0,0,0,1,0,0,0,0,1},

{1,0,1,0,0,0,1,0,0,1},

{1,0,1,1,1,0,1,1,0,1},

{1,1,0,0,0,0,0,0,0,1},

{1,1,1,1,1,1,1,1,1,1}

    };



输入

无输入

输出

1 1
1 2
2 2
3 2
3 1
4 1
5 1
5 2
5 3
6 3
6 4
6 5
5 5
4 5
4 6
4 7
3 7
3 8
4 8
5 8
6 8
7 8
8 8

样例输出

1 1
1 2
2 2
3 2
3 1
4 1
5 1
5 2
5 3
6 3
6 4
6 5
5 5
4 5
4 6
4 7
3 7
3 8
4 8
5 8
6 8
7 8
8 8

#include<iostream>
using namespace std;
int a[10][10] =
{     
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,0,0,1,1,0,0,1},
    {1,0,1,1,1,0,0,0,0,1},
    {1,0,0,0,1,0,0,0,0,1},
    {1,0,1,0,0,0,1,0,0,1},
    {1,0,1,1,1,0,1,1,0,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
int p[100],q[100];
bool vis[13][13];
bool is=0;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int len;
void dfs(int x,int y,int step){
    if(is==1){
        return;
    }
    if(x==8&&y==8){
        is=1;
        p[step]=x;
        q[step]=y;
        len=step;
        return;
    }
    vis[x][y]=1;
    p[step]=x;
    q[step]=y;
    for(int i=0;i<4;i++){
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(a[xx][yy]==1||vis[xx][yy]==1){
            continue;
        }
        dfs(xx,yy,step+1);
    }
    vis[x][y]=0;
}
int main(){
    dfs(1,1,0);
    for(int i=0;i<=len;i++){
        cout<<p[i]<<" "<<q[i]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/80761682