走迷宫

题目

输入

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

结果
1
0
4

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 10

int map[maxn][maxn],vis[maxn][maxn],n,m,k;

void BFS(int u,int v)
{
    int x,y;
    int a[]= {0,0,-1,1},b[]= {-1,1,0,0};
    for(int i=0; i<4; i++)
    {
        x=u+a[i];
        y=v+b[i];
        if(x==n && y==m)
            k++;
        else if(x<=n && x>0 && y<=m && y>0)
        {
            if(!map[x][y] && !vis[x][y])
            {
                vis[x][y]=1;
                BFS(x,y);
            }
        }
    }
    vis[u][v]=0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        k=0;
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                scanf("%d",&map[i][j]);
        vis[1][1]=1;
        BFS(1,1);
        printf("%d
",k);
    }
    return 0;
}

  

  

原文地址:https://www.cnblogs.com/guoyongzhi/p/3233032.html