C - test of the stack and the queue data structure ten: Maze

Description

A maze of lattice composed of n * m, the starting point is (1, 1), the end point is (n, m), each may take any of the four directions step, and some can not move the lattice, starting from the request after the end of each grid to take the number of law at most once.
Input

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

For each set of test data:

The first line of two integers n, m, n * m represents labyrinths squares. (1 <= n, m <= 6, (n, m)! = (1, 1)) Next n lines, each line number m. Where the i-th row and j is the number 0 for i-th row j-th lattice can go, otherwise it is 1 indicates that the grid can not go, enter the start and end points are to ensure that all can go.

Test data between any two groups are separated by a blank line.
Output

For each test case, the output of an integer R, which R represents a species moves.

Sample

Input

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

1
0
4

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int Map[110][110],s[110][110];
int n,m,num;
void dfs(int x,int y)
{
    if(x<1||y<1||x>n||y>m||s[x][y]||Map[x][y])//限制xy能走的部分
        return ;
    else
    {
        if(x == n&&y == m)//走到终点了
        {
            num ++;//可以走通的路径加1
            return ;
        }
        else
        {
            Map[x][y] = 1;//标记这个边已经走过了
            dfs(x-1,y);
            dfs(x+1,y);
            dfs(x,y-1);
            dfs(x,y+1);
            Map[x][y] = 0;//道路不通返回的时候重新置0
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        num = 0;
        memset(Map,0,sizeof(Map));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                scanf("%d",&s[i][j]);
        }
        dfs(1,1);
        printf("%d\n",num);
    }
    return 0;
}

Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104882304