• 【Codevs1215】迷宫


    Description

    在N*N的迷宫内,“#”为墙,“.”为路,“s”为起点,“e”为终点,一共4个方向可以走。从左上角((0,0)“s”)位置处走到右下角((n-1,n-1)“e”)位置处,可以走通则输出YES,不可以走则输出NO。

    Input

    输入的第一行为一个整数m,表示迷宫的数量。 
    其后每个迷宫数据的第一行为一个整数n(n≤16),表示迷宫的边长,接下来的n行每行n个字符,字符之间没有空格分隔。

    Output

    输出有m行,每行对应的迷宫能走,则输出YES,否则输出NO。

    Sample Input

    1 
    7
    s...##.
    .#.....
    .......
    ..#....
    ..#...#
    ###...#
    ......e

    Sample Output

    YES

    题解

    #include<iostream>
    using namespace std;
    char a[20][20];
    bool vis[20][20];
    int xx[5]={0,-1,1,0,0};
    int yy[5]={0,0,0,-1,1};
    int m,n,judge;
    void dfs(int x,int y)
    {
        vis[x][y]=true;
        if (a[x][y] == 'e')
        {
            judge = 1;
            return;
        }
        for (int i=1;i<=4;i++)
            if (x+xx[i]>0 && x+xx[i]<=n && y+yy[i]>0 && y+yy[i]<=n)
                if (a[x+xx[i]][y+yy[i]] != '#' && !vis[x+xx[i]][y+yy[i]])
                    dfs(x+xx[i],y+yy[i]);
    }
    int main()
    {
        cin>>m>>n;
        for (int i=1;i<=m;i++)
        {
            judge=0;
            for (int j=1;j<=n;j++)
                for (int k=1;k<=n;k++)
                    cin>>a[j][k];
            dfs(1,1);
            if (judge) cout<<"YES";
                else cout<<"NO";
        }
        return 0;
    }
  • 相关阅读:
    UIView的clipsToBounds属性,layoutSubViews及触摸事件传递(默认情况下)总结
    ISO中运行时简单使用及KVC补充
    IOS中UISearchBar的使用
    oc的block
    oc的协议(protocol)
    oc的分类category
    oc内存的理解
    oc笔记(转载)
    oc对象中属性总结
    servlet,struts1,struts2,spring
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5203803.html
Copyright © 2020-2023  润新知