Red and Black

http://acm.hdu.edu.cn/showproblem.php?pid=1312

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std ;
 4 char map[21][21] ;
 5 bool v[21][21] ;
 6 int w, h ;
 7 int ans ;
 8 int dir[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1} };
 9 void dfs( int x, int y )
10 {
11     for( int i=0; i<4; i++ )
12     {
13         int xx = x + dir[i][0] ;
14         int yy = y + dir[i][1] ;
15         if( xx<1 || xx>h || yy<1 || yy>w )
16             continue ;
17         if( map[xx][yy]=='.' && !v[xx][yy] )
18         {
19             ans ++;
20             v[xx][yy] = 1;
21             dfs( xx, yy );
22         }
23     }
24 }
25 int main()
26 {
27     int x, y;
28     while(cin>>w>>h,(w+h))
29     {
30         for( int i=1; i<=h; i++ )
31         {
32             for( int j=1; j<=w; j++ )
33             {
34                 cin>>map[i][j] ;
35                 if( map[i][j] == '@' )
36                 {
37                     x = i ;
38                     y = j ;
39                 }
40             }
41         }
42         ans = 1;
43         memset( v, 0, sizeof( v ) ) ;
44         v[x][y] = 1 ;
45         dfs( x, y ) ;
46         cout<<ans<<endl ;
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/yelan/p/2934218.html