Oil Deposits

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

题意:就是找出有多少块有石油的区域,就是数组中的@,这边相邻指的是是周围的八个位置。

题解:  dfs,从一块油田的位子开始,朝着与他相邻的8个方向收索,遇到油田就把它变成不是油田,并往下继续。遍历一下,就知道有多少了

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 char map[102][102];
 7 int m,n;
 8 void dfs(int i,int j){
 9      if(i<1||i>m||j<1||j>n)
10        return;
11       if(map[i-1][j]=='@')
12       {  map[i-1][j]='*';
13           dfs(i-1,j);
14            }
15        if(map[i+1][j]=='@')
16       {  map[i+1][j]='*';
17           dfs(i+1,j);
18            }
19         if(map[i][j+1]=='@')
20       {  map[i][j+1]='*';
21           dfs(i,j+1);
22            }
23       if(map[i][j-1]=='@')
24         {  map[i][j-1]='*';
25           dfs(i,j-1);
26            }
27            
28       if(map[i+1][j+1]=='@')
29       {  map[i+1][j+1]='*';
30           dfs(i+1,j+1);
31            }
32        if(map[i-1][j+1]=='@')
33       {  map[i-1][j+1]='*';
34           dfs(i-1,j+1);
35            }
36       if(map[i-1][j-1]=='@')
37       {  map[i-1][j-1]='*';
38           dfs(i-1,j-1);
39            }
40         if(map[i+1][j-1]=='@')
41       {  map[i+1][j-1]='*';
42           dfs(i+1,j-1);
43            }
44            
45 }
46 int main(){
47      int count;
48    while(~(scanf("%d%d",&m,&n))&&m!=0){
49      count=0;
50      memset(map,0,sizeof(map));
51       for(int i=1;i<=m;i++)
52          for(int j=1;j<=n;j++){
53            cin>>map[i][j];
54          }
55        for(int i=1;i<=m;i++)
56           for(int j=1;j<=n;j++)
57           { if(map[i][j]=='@'){
58               count++;
59               dfs(i,j);
60            }
61           }
62           printf("%d\n",count);
63     }
64   }
View Code

 

posted on 2013-10-13 11:51  天依蓝  阅读(159)  评论(0编辑  收藏  举报

导航