「LuoguP1238」 走迷宮

Description


有一個m*n格的迷宮(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件讀入這m*n個數據和起始點、結束點(起始點和結束點都是用兩個數據來描述的,分別表示這個點的行號和列號)。現在要你編程找出所有可行的道路,要求所走的路中沒有重複的點,走時只能是上下左右四個方向。如果一條路都不可行,則輸出相應信息(用-l表示無路)。

優先順序:左上右下

Input


第一行是兩個數m,n(1 < m,n < 15),接下來是m行n列由1和0組成的數據,最後兩行是起始點和結束點。

Output


所有可行的路徑,描述一個點時用(x,y)的形式,除開始點外,其他的都要用“一>”表示方向。

如果沒有一條可行的路則輸出-1。

Sample Input


5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6

Sample Output


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

題解


按題意暴搜即可。

第一次忘了給(1,1)打經歷過的tag,卡掉了一次

#include<cstdio>
#include<iostream>
using namespace std;
bool sf[17][17];
int sx,sy,tx,ty;
int m,n;
int mx[5]={0,0,-1,0,1};
int my[5]={0,-1,0,1,0};
int stack[307][2];
int tos=0;
bool ss[17][17];
int flag=0;
void print()
{
    flag++;
    printf("(%d,%d)",sx,sy);
    for(int i=1;i<=tos;++i)
    printf("->(%d,%d)",stack[i][0],stack[i][1]);
    cout<<endl;
    return;
}
void search(int x,int y)
{
    for(int c=1;c<=4;++c)
    {
        x+=mx[c],y+=my[c];
        if(sf[x][y]&&!ss[x][y])
        {
            //cout<<x<<" "<<y<<endl;
            stack[++tos][0]=x;stack[tos][1]=y;
            if(x==tx&&y==ty){print();}
            else {ss[x][y]=1;search(x,y);ss[x][y]=0;}
            tos--;
        }
        x-=mx[c],y-=my[c];
    }
    return;
}
int main()
{
    cin>>m>>n;
    for(int i=1;i<=m;++i)
    for(int j=1;j<=n;++j)
    cin>>sf[i][j];cin>>sx>>sy>>tx>>ty;
    ss[sx][sy]=1;
    search(sx,sy);
    if(!flag)cout<<-1;
    return 0;
}
發佈了31 篇原創文章 · 獲贊 5 · 訪問量 1874
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章