迷宫可行路径数

题目描述

现有一个n∗m大小的迷宫,其中1表示不可通过的墙壁,0表示平地。每次移动只能向上下左右移动一格(不允许移动到曾经经过的位置),且只能移动到平地上。求从迷宫左上角到右下角的所有可行路径的条数。

输入描述

第一行两个整数n、m​​​(2≤n≤5,2≤m≤5​​​),分别表示迷宫的行数和列数;

接下来n行,每行m个整数(值为01),表示迷宫。

输出描述

一个整数,表示可行路径的条数。

输入样例

cpp 复制代码
3 3 
0 0 0
0 1 0
0 0 0

输出样例

cpp 复制代码
2

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt = 0;
int vis[1005][1005],Map[1005][1005];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool isValid(int x,int y){
	return x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&Map[x][y]==0;
}
void dfs(int x,int y){
	if(x==n-1&&y==m-1){
		cnt++;
		return;
	}
	vis[x][y] = 1;
	for(int i = 0;i<4;i++){
		int nextx = x+dx[i];
		int nexty = y+dy[i];
		if(isValid(nextx,nexty)){
			dfs(nextx,nexty);
		}
		
	}
	vis[x][y] = 0;
}
int main(){
	cin>>n>>m;
	for(int i = 0;i<n;i++){
		for(int j = 0;j<m;j++){
			Map[i][j] = 0;
		}
	}
	for(int i = 0;i<n;i++){
		for(int j = 0;j<m;j++){
			cin>>Map[i][j];
		}
	}
	dfs(0,0);
	cout<<cnt<<endl;
	
	
}
相关推荐
练习时长一年16 小时前
Leetcode热题100(跳跃游戏 II)
算法·leetcode·游戏
小白菜又菜1 天前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
wuhen_n1 天前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo1 天前
leetcode 2483
数据结构·算法·leetcode
Xの哲學1 天前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
大头流矢1 天前
归并排序与计数排序详解
数据结构·算法·排序算法
油泼辣子多加1 天前
【信创】算法开发适配
人工智能·深度学习·算法·机器学习
一路往蓝-Anbo1 天前
【第20期】延时的艺术:HAL_Delay vs vTaskDelay
c语言·数据结构·stm32·单片机·嵌入式硬件
Aaron15881 天前
AD9084和Versal RF系列具体应用案例对比分析
嵌入式硬件·算法·fpga开发·硬件架构·硬件工程·信号处理·基带工程
laocooon5238578861 天前
插入法排序 python
开发语言·python·算法