zoukankan      html  css  js  c++  java
  • 「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;
    }
  • 相关阅读:
    Atitit.Java exe bat  作为windows系统服务程序运行
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结
  • 原文地址:https://www.cnblogs.com/qwerta/p/9379751.html
Copyright © 2011-2022 走看看