HDU1312

View Code
 1 /*
 2 dfs 从一个点出发最多能走多少步
 3 用bfs较为麻烦
 4 */
 5 #include<stdio.h>
 6 #include<string.h>
 7 #include<stdlib.h>
 8 #include<algorithm>
 9 #include<iostream>
10 #include<queue>
11 //#include<map>
12 #include<math.h>
13 using namespace std;
14 typedef long long ll;
15 //typedef __int64 int64;
16 const int maxn = 25;
17 const int inf = 0x7fffffff;
18 const int pi=acos(-1.0);
19 struct node{
20     int x,y;
21 };
22 
23 char mat[ maxn ][ maxn ];
24 int vis[ maxn ][ maxn ];
25 const int dx[]={0,0,1,-1};
26 const int dy[]={1,-1,0,0};
27 int ans;
28 
29 void dfs( int x,int y,int n,int m ){
30     ans++;    //if( sum>ans ) {ans=sum;}
31     for( int k=0;k<4;k++ ){
32         int tx,ty;
33         tx=x+dx[ k ],ty=y+dy[ k ];
34         if( tx<0||tx>=n||ty<0||ty>=m ) continue;
35         if( mat[ tx ][ ty ]=='#'||vis[ tx ][ ty ]==1 ) continue;
36         //printf("tx:%d ty:%d sum:%d \n",tx,ty,sum);
37         vis[ tx ][ ty ]=1;
38         dfs( tx,ty,n,m );
39         //vis[ tx ][ ty ]=0;
40     }
41     return ;
42 }
43 
44 int main(){
45     int n,m;
46     while( scanf("%d%d",&m,&n)==2 ,n+m ){
47         for( int i=0;i<n;i++ ) scanf("%s",mat[ i ]);
48         node s;
49         memset( vis,0,sizeof(vis) );
50         for( int i=0;i<n;i++ )
51             for( int j=0;j<m;j++ )
52                 if( mat[ i ][ j ]=='@') {
53                     s.x=i;
54                     s.y=j;
55                     break;
56                 }
57         ans=0;
58         vis[ s.x ][ s.y ]=1;
59         dfs( s.x,s.y,n,m );
60         printf("%d\n",ans);
61     }
62     return 0;
63 }
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/2813200.html