欢迎使用CSDN-markdown编辑器

来源:互联网 发布:怎么查看手机mac地址 编辑:程序博客网 时间:2024/04/30 05:48

走迷宫

Time Limit: 1000MS Memory Limit: 65536KB
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 种走法。

Example Input

3
2 2
0 1
0 0
2 2
0 1
1 0
2 3
0 0 0
0 0 0

Example Output

1
0
4

Hint

Author

代码实例

#include <stdio.h>#include <stdlib.h>#include <memory.h>int a[10][10];int b[10][10];int n, m, ans=0;void bre(int i, int j){    if(a[i][j]||b[i][j])    {        return ;    }    if(i==n&&j==m)    {        ans++;        return ;    }    b[i][j] = 1;    bre(i+1,j);    bre(i-1,j);    bre(i,j+1);    bre(i,j-1);    b[i][j] = 0;}int main(){    int t, i, j;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n, &m);        memset(b,0,sizeof(b));        for(i=0;i<=n+1;i++)        {            for(j=0;j<=m+1;j++)            {                a[i][j]=1;            }        }        for(i=1; i<=n; i++)        {            for(j=1; j<=m; j++)            {                scanf("%d", &a[i][j]);            }        }        ans=0;        bre(1,1);        if(t==0)            printf("%d\n", ans);        else            printf("%d\n", ans);    }    return 0;}
原创粉丝点击