HDU 1241

这是我做过最简单的一道搜索题了、可我开始想的时候觉得特别复杂......    - - 好吧、 我确实是很水

题意:输入一张地图,@代表油田,如果一个@周边8个方向存在@,那么它就是一个油田,对于每个数据输出总的油田数

思路:深搜,每找到一个油田就往其他8个方向搜;

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int qq=100+10;
 5 char map[qq][qq];
 6 int n,m;
 7 void dfs(int y,int x)
 8 {
 9     if(x<0||y<0||x>=m||y>=n||map[y][x]!='@')    return;    //不为@就可以直接返回了 
10     else{                                    //否则往八个方向继续找 
11         map[y][x]='*';            //如果为@就要标记一下,不能在这个地方继续找了 
12         dfs(y+1,x);    
13         dfs(y-1,x);
14         dfs(y,x+1);
15         dfs(y,x-1);
16         dfs(y-1,x+1);
17         dfs(y-1,x-1);
18         dfs(y+1,x+1);
19         dfs(y+1,x-1);
20     }
21     return;
22 }
23 int main()
24 {
25     while(cin >> n >> m&&(n||m)){
26         int tot=0;
27         for(int i=0;i<n;++i)
28             for(int j=0;j<m;++j)
29                 cin >> map[i][j];
30         for(int j,i=0;i<n;++i)
31             for(j=0;j<m;++j)
32                 if(map[i][j]=='@'){
33                     dfs(i,j);
34                     ++tot;
35                 }
36         cout << tot << endl;
37     }
38 }
原文地址:https://www.cnblogs.com/sasuke-/p/5136815.html