SDUT ———— 2448 DFS走迷宫

#include<bits/stdc++.h>

using namespace std;

int matrix[7][7];
int cout1 = 0;
int n,m;
int visit[7][7];

inline bool judge(int e1,int e2 )
{
    return    (e1 > 0 ) && ( e1 <= n) &&(e2 > 0)&&( e2 <=m);
}

int  DFS(int e1,int e2)
{
    if(n == e1 && m ==e2){

            cout1 ++;
            return 1;

    }
       visit[e1][e2] = 7;
        if( !visit[e1-1][e2] && judge(e1-1,e2)&&!matrix[e1-1][e2])  DFS(e1-1,e2);
        if( !visit[e1+1][e2] && judge(e1+1,e2)&&!matrix[e1+1][e2]) DFS(e1+1,e2);
        if( !visit[e1][e2-1] && judge(e1,e2-1)&&!matrix[e1][e2-1])  DFS(e1,e2-1);
        if( !visit[e1][e2+1] && judge(e1,e2+1)&&!matrix[e1][e2+1])  DFS(e1,e2+1);
        visit[e1][e2] = 0;

}

int main(){
    int T;
    cin >>T;
    while (T -- > 0){
            cout1 =0;
       memset(visit,0,sizeof(visit));
        cin>>n>>m;
        for(int i =1;i <=n;i++)
            for(int j = 1; j <= m;j++)
               cin >> matrix[i][j];


            DFS(1,1);
           cout << cout1<<endl;

    }
}

 

posted on 2019-04-02 14:28  TigerDZ  阅读(99)  评论(0编辑  收藏  举报

导航