题解 | 迷宫寻路

迷宫寻路

https://www.nowcoder.com/practice/0c8930e517444d04b426e9703d483ed4

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N = 0;
int M = 0;
char mesh[100][100];
char vis[100][100];
int CheckPoint(char mesh[100][100], int x, int y);

int CheckPoint(char mesh[100][100], int x, int y)
{
    if((x == 1) && (y == 1))
        return 0;
    else
    {
        if(vis[x][y] == 1)
            return 1;
        else
        {
            vis[x][y] = 1;
            if((x > 1) && (mesh[x-1][y] == '.'))
            {
                if(0 == CheckPoint(mesh, x - 1, y))
                    return 0;
            }

            if((x < M) && (mesh[x+1][y] == '.'))
            {
                if(0 == CheckPoint(mesh, x + 1, y))
                    return 0;
            }

            if((y > 1) && (mesh[x][y-1] == '.'))
            {
                if(0 == CheckPoint(mesh, x, y - 1))
                    return 0;
            }

            if((y < N) && (mesh[x][y+1] == '.'))
            {
                if(0 == CheckPoint(mesh, x, y + 1))
                    return 0;
            }
        }

    }

    return 1;
}

int main()
{
    scanf("%d %d", &M, &N);

    memset(mesh, 0, sizeof(char)*10000);
    for(int loop = 1; loop < M; ++loop)
    {
        scanf("%s", &(mesh[loop][1]));
    }

    if(0 == CheckPoint(mesh, M, N))
    {
        printf("Yes");
    }
    else {
        printf("No");
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务