SDUT 1570 C 旅行(DFS)

点我看题目

题意 : 中文不详述。

思路 :就是DFS一下,只要到达终点条数就加1,然后再注意一下方向,因为我就是没注意方向WA了,只能向上向右走,x是行,所以向上是x-1,向右是y+1,因为我没弄好,结果就废了。。。。。。

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <algorithm>



using namespace std;



int m,n,sx,sy,ex,ey ;

int mapp[30][30] ;

int vis[30][30] ;

int dire[2][2] = {{0,1},{-1,0}} ;

int cnt ;



void DFS(int x,int y)

{

    if(x == ex && y == ey)

      {

           cnt++ ;

           return ;

      }

    else

    {

        for(int i = 0 ; i < 2 ; i++)

        {

            int xx = x+dire[i][0] ;

            int yy = y+dire[i][1] ;

            if(!mapp[xx][yy] && xx >= 1 && xx <= m && yy >= 1 && yy <= n && !vis[xx][yy])

            {

                vis[xx][yy] = 1 ;

                DFS(xx,yy) ;

                vis[xx][yy] = 0 ;

            }

        }

    }

}

int main()

{

    while(~scanf("%d %d",&m,&n))

    {

        memset(vis,0,sizeof(vis)) ;

        for(int i = 1 ; i <= m ; i++)

            for(int j = 1 ; j <= n ; j++)

                scanf("%d",&mapp[i][j]) ;

        sx = m,sy = 1 ;

        ex = 1,ey = n ;

        vis[sx][sy] = 1;

        cnt = 0 ;

        DFS(sx,sy) ;

        printf("%d\n",cnt) ;

    }

    return 0;

}
View Code

 

你可能感兴趣的:(DFS)