走迷宫

走迷宫
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

有一个m*n格的迷宫(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,输入这m*n个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。
Input

第一行是两个数m,n(1< m, n< 15),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。
Output

所有可行的路径,输出时按照左上右下的顺序。描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示。如果没有一条可行的路则输出-1。
Example Input

5 4
1 1 0 0
1 1 1 1
0 1 1 0
1 1 0 1
1 1 1 1
1 1
5 4
Example Output

(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
Hint

Author

#include<iostream>

using namespace std;

struct no
{
    int x,y;
}s[10000000];
int l=0;
int a[20][20];
int h,w;
int x=0,y=0;
int sum = 0;
int map(int n,int m)
{
    if(n<0||m<0||n>=h||m>=w)
    {
        return 0;
    }
    if(a[n][m] == 1)
    {
        s[l].y = n;
        s[l].x = m;
        l++;
        int ok = 0;
        if(n==y-1&&m==x-1)
        {
            for(int i=0;i<l;i++)
                if(i==0)
                    cout<<"("<<s[i].y+1<<","<<s[i].x+1<<")";
                else
                    cout<<"->("<<s[i].y+1<<","<<s[i].x+1<<")";
            sum++;
            cout<<endl;
            l--;
            return 1;
        }
        a[n][m] = 2;
        if(m-1>=0&&a[n][m-1]==1)
        {
            if(map(n,m-1))
                ok = 1;
        }
        if(n-1>=0&&a[n-1][m]==1)
        {
            if(map(n-1,m))
                ok = 1;
        }

        if(m+1<w&&a[n][m+1]==1)
        {
            if(map(n,m+1))
                ok = 1;
        }

        if(n+1<h&&a[n+1][m]==1)
            if(map(n+1,m))
                ok = 1;
        a[n][m] = 1;
        l--;
        if(ok)
            return 1;
    }
}

int main()
{
    int k1,k2;
    cin>>h>>w;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>a[i][j];
        }
    }
    cin>>k1>>k2;
    cin>>y>>x;
    map(k1-1,k2-1);
    if(sum==0)
        cout<<"-1";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值