sdutacm-走迷宫

来源:互联网 发布:mac搜狗五笔输入法 编辑:程序博客网 时间:2024/05/22 02:23

走迷宫

Time Limit: 1000MSMemory Limit: 65536KB

SubmitStatistic

ProblemDescription

一个由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种走法。

ExampleInput

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

ExampleOutput

1
0
4

Hint

 

Author

#include <iostream>#include<bits/stdc++.h>using namespace std;int n,m,r;int ma[10][10],book[10][10];void dfs(int x,int y){   int next[4][2] ={{0,1},{1,0},{0,-1},{-1,0}};   int tx,ty,k;  if(x==n&&y==m)   {      r++;      return ;   }   for(k=0;k<=3;k++)   {    tx = x+next[k][0];    ty = y+next[k][1];    if(tx<1||tx>n||tx<1||ty>m)    {     continue;    }    if(ma[tx][ty]==0&&book[tx][ty]==0)    {    book[tx][ty]=1;    dfs(tx,ty);    book[tx][ty]=0;    }   }  return ;}int main(){    int t;    scanf("%d",&t);    while(t--)    {    memset(ma,1,sizeof(ma));    memset(book,0,sizeof(book));    cin>>n>>m;    for(int i=1;i<=n;i++)    for(int j=1;j<=m;j++)    cin>>ma[i][j];    r = 0;    book[1][1] = 1;    dfs(1,1);    cout<<r<<endl;    }    return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 452msTake Memory: 152KBSubmit time: 2017-02-15 11:23:39****************************************************/

0 0