首页 技术 正文
技术 2022年11月18日
0 收藏 316 点赞 3,168 浏览 996 个字

数据结构实验之栈与队列十:走迷宫

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方向任意走一步,并且有些格子是不能走动,求从起点到终点经过每个格子至多一次的走法数。

Input

第一行一个整数T 表示有T 组测试数据。(T <= 110)

对于每组测试数据:

第一行两个整数n, m,表示迷宫有n * m 个格子。(1 <= n, m <= 6, (n, m) !=(1, 1) ) 接下来n 行,每行m 个数。其中第i 行第j 个数是0 表示第i 行第j 个格子可以走,否则是1 表示这个格子不能走,输入保证起点和终点都是都是可以走的。

任意两组测试数据间用一个空行分开。

Output

对于每组测试数据,输出一个整数R,表示有R 种走法。

Sample Input

3

2 2

0 1

0 0

2 2

0 1

1 0

2 3

0 0 0

0 0 0

Sample Output

1

0

4

比较疑惑这道题为什么会分到这里,这是一道简单的DFS题,上学期的动态规划有类似的题目,图的知识点,可以去看看相应知识。

另外这道题没用栈和队列

#include <stdio.h>
#include <stdlib.h>
#include <string.h>int s[10][10],f[10][10],num,n,m;
int next[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};void DFS(int x,int y)
{
if(x==n-1&&y==m-1)
{
num++;
return;
}
int dx,dy,i;
for(i=0;i<4;i++)
{
dx = x + next[i][0];
dy = y + next[i][1];
if(dx>=0&&dy>=0&&dx<n&&dy<m&&s[dx][dy]!=1)
{
if(!f[dx][dy])
{
f[dx][dy] = 1;
DFS(dx,dy);
f[dx][dy] = 0;
}
}
}
}int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
f[i][j] = 0;
scanf("%d",&s[i][j]);
}
num = 0;
f[0][0] = 1;
DFS(0,0);
printf("%d\n",num);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893