迷宫问题dfs

J. 迷宫问题(migong) [ Problem 1737 ] [ Discussion ]
Description
设有一个N∗N(2≤N<10)方格的迷宫,入口和出口分别在左上角和右上角。迷宫格子中分别放0和1,0表示可通,1表示不能,入口和出口处肯定是0。迷宫走的规则如下所示:即从某点开始,有八个方向可走,前进方格中数字为0时表示可通过,为1时表示不可通过,要另找路径。找出所有从入口(左上角)到出口(右上角)的路径(不能重复),输出路径总数,如果无法到达,则输出0。

Samples
Input Copy
3
0 0 0
0 1 1
1 0 0​
Output
2
Source
信息学一本通 初赛篇 算法部分
Discussions
No more discussions

dfs 一条路走到黑 深度优先
More>>

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
ll read(){ll res = 0, ch, flag = 0;if((ch = getchar()) == '-')flag = 1;else if(ch >= '0' && ch <= '9')res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}
const int maxn =1e6+199 ;
ll sum=0,maxa=-1;
ll n,m,k,w,ans=0,cnt=0;
ll a[300][300];
ll b[300][300];
ll dis[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
ll mod=1e9+7;
struct node{
        int x,y;
}st,en;
queue<node>p;
void dfs(int xx,int yy)
{
    if(xx==1&&yy==n)
    {
        cnt++;
        return;
    }
    int x,y;
    for(int i=0;i<=7;i++)
    {
        x=xx+dis[i][0];
        y=yy+dis[i][1];
        if(x>=1&&x<=n&&y>=1&&y<=n&&a[x][y]==0&&b[x][y]==0)
        {
            b[x][y]=1;///我先走这条道
            dfs(x,y);
            b[x][y]=0;///不走这个
        }
    }

    return;
}
int main()
{

    n=read();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    }
    b[1][1]=1;
    dfs(1,1);
    cout<<cnt;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个Python版的深度优先搜索算法生成迷宫问题的示例代码: ```python import random def generate_maze(rows, cols): # 创建一个 rows 行 cols 列的二维数组,初始化为全 1 maze = [[1] * cols for _ in range(rows)] # 将起点设置为 (0, 0),并将其标记为 0 start_row, start_col = 0, 0 maze[start_row][start_col] = 0 # 使用深度优先搜索算法生成迷宫 dfs(maze, start_row, start_col) return maze def dfs(maze, row, col): # 定义四个方向:上、下、左、右 directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # 随机打乱方向的顺序 random.shuffle(directions) # 遍历每个方向 for direction in directions: # 计算新的坐标 new_row, new_col = row + direction[0], col + direction[1] # 判断新坐标是否越界或已经被访问过 if (new_row < 0 or new_row >= len(maze) or new_col < 0 or new_col >= len(maze[0]) or maze[new_row][new_col] == 0): continue # 打通当前坐标与新坐标之间的墙壁 if direction == (0, -1): # 左 maze[row][col-1] = 0 elif direction == (0, 1): # 右 maze[row][col+1] = 0 elif direction == (-1, 0): # 上 maze[row-1][col] = 0 elif direction == (1, 0): # 下 maze[row+1][col] = 0 # 递归访问新坐标 dfs(maze, new_row, new_col) # 生成一个 5x5 的迷宫 maze = generate_maze(5, 5) # 打印迷宫 for row in maze: print(row) ``` 这个示例代码可以生成一个指定大小的迷宫,并使用深度优先搜索算法生成迷宫的路径。其,生成迷宫的主要逻辑在 `generate_maze()` 函数,它首先创建一个二维数组表示迷宫,然后使用深度优先搜索算法从起点开始访问迷宫的所有格子,并打通相邻格子之间的墙壁,最终生成迷宫的路径。在这个示例代码,我们使用随机打乱方向的顺序来增加迷宫的随机性。最后,我们打印生成的迷宫,查看迷宫的路径和墙壁的布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值