题解 | #迷宫问题#

迷宫问题

https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

#include <stdio.h>
int tace[100][2];  
int realtace[100][2];
int minstep = 100;
int maze[10][10];
int tage[10][10] = { 0 };
int p, q;
int n, m;


void dfs(int x, int y, int maze[10][10], int step)
{
    tace[step][0] = x;
    tace[step][1] = y;
    if (x == q && y == p)
    {
	  // 最短路径
        if (step < minstep)
        {
            minstep = step;
            //printf("%d   ", step);
            for (int i = 0; i <= minstep; i++)
            {
                realtace[i][0] = tace[i][0];
                realtace[i][1] = tace[i][1];
                //printf("%d %d\n", realtace[i][0], realtace[i][1]);
            }
        }
        return;
    }
    if (x < 0 || y < 0 || y > p || x > q)
        return;
    // 右边
    //printf("%d %d\n", x, y);
    
    if (maze[x ][y +1] == 0 && tage[x][y + 1] == 0)
    {
        tage[x][y + 1] = 1;
        dfs(x, y+1, maze, step + 1);
    }
    // 下边
    if (maze[x + 1][y] == 0 && tage[x + 1][y] == 0)
    {
        tage[x + 1][y] = 1;
        dfs(x + 1, y , maze, step + 1);
    }
    // 左边
     if (maze[x][y -1] == 0 && tage[x][y -1] == 0)
    {
        tage[x][y - 1] = 1;
        dfs(x, y -1, maze, step + 1);
    }
    // 上边
    if (maze[x -1][y] == 0 && tage[x - 1][y] == 0)
    {
        tage[x - 1][y] = 1;
        dfs(x - 1, y, maze, step + 1);
    }
    return;
}


int main() {

    scanf("%d%d", &n, &m);

    p = m - 1;
    q = n - 1;
    
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%d", &maze[i][j]);
    //printf("%d  %d", p, q);
    dfs(0, 0, maze, 0);
  	tage[0][0] = 1;
    for (int i = 0; i <= minstep; i++)
    {
        printf("(%d,%d)\n", realtace[i][0], realtace[i][1]);
    }
    return 0;
}

全部评论

相关推荐

头像
11-03 16:48
已编辑
百度_高级研发工程师
事实是检验真理的唯一标准。&nbsp;无论我们怎么去说,去讲述,去证明,都抵不过一个offer来得实在,无论我们怎么去复现求职中的摸爬滚打、扒皮抽筋、狼狈不堪,都抵不过你在简历写上大厂的名字(外包不算)。&nbsp;所以在我求职期间,我什么话都不说,什么话都不讲,因为没有意义,虽然我总讲过程才是意义,但只有当你上岸的那一刻,你才有资格回想在水里的挣扎,只有等你出了山,你才知道山的全貌。&nbsp;我为什么一定要离开华为OD,难道它不稳定吗,不能赚钱吗。为了证明自己,那肯定有的。其实更多的是印证我的认知是否真的正确。&nbsp;(给不了解我的人交代一下背景,在下双非一本,gap一年,华为OD外包,摸爬滚打4个月,艰难上岸百度正编)一、...
先锋战士:说得很真诚。鄙视链自古有之,学历,家庭背景,财富,权利。从小有之,小学羡慕那些当班委的,中学羡慕那些学生会的,高中羡慕尖子班拿教学金的,大学羡慕高绩点,毕业了羡慕进大厂的。工作了,又羡慕高职级的,再后来又羡慕别人早早结婚的。我想表达的观点很简单,无论是华为od还是百度,都是经历,没有孰高孰低,为了抵达下一个风景,总会付出更多东西,但不就是人生吗?正如登山,每个阶段的山,都要想办法攀登,在博主的文字中,见到了坚持和积极寻找问题解决办法的心态
学历对求职的影响
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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