UVa572 - Oil Deposits

解题思路:好久没写搜索了,练练手,陶冶情操。不多说,直接贴代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn = 105;
 6 int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1,
 7                  0, 1, 1, -1, 1, 0, 1, 1};
 8 char gg[maxn][maxn];
 9 int m, n;
10 
11 void DFS(int x, int y, int id)
12 {
13     if(x < 1 || x > m || y < 1 || y > n || gg[x][y] == '*') return ;
14     gg[x][y] = '*';
15     for(int i = 0; i < 8; i++)
16     {
17         int xx = x + dir[i][0];
18         int yy = y + dir[i][1];
19 
20         if(xx < 1 || xx > m || yy < 1 || yy > n || gg[xx][yy] == '*') continue;
21         DFS(xx, yy, id);
22     }
23     return ;
24 }
25 int main()
26 {
27     int cnt;
28     while(~scanf("%d %d", &m, &n) && m)
29     {
30         memset(gg, '#', sizeof(gg));
31         for(int i = 1; i <= m; i++)
32         for(int j = 1; j <= n; j++) scanf(" %c", &gg[i][j]);
33 
34         cnt = 0;
35         for(int i = 1; i <= m; i++)
36         for(int j = 1; j <= n; j++)
37         if(gg[i][j] == '@') DFS(i, j, ++cnt);
38         printf("%d\n", cnt);
39     }
40     return 0;
41 }
View Code

 

posted on 2015-09-25 22:29  改写历史,倾尽天下  阅读(93)  评论(0编辑  收藏  举报

导航