标签:
1 /*
2 DFS:油田问题,一道经典的DFS求连通块。当初的难题,现在看上去不过如此啊
3 */
4 /************************************************
5 Author :Running_Time
6 Created Time :2015-8-4 10:11:11
7 File Name :HDOJ_1241.cpp
8 ************************************************/
9
10 #include <cstdio>
11 #include <algorithm>
12 #include <iostream>
13 #include <sstream>
14 #include <cstring>
15 #include <cmath>
16 #include <string>
17 #include <vector>
18 #include <queue>
19 #include <deque>
20 #include <stack>
21 #include <list>
22 #include <map>
23 #include <set>
24 #include <bitset>
25 #include <cstdlib>
26 #include <ctime>
27 using namespace std;
28
29 #define lson l, mid, rt << 1
30 #define rson mid + 1, r, rt << 1 | 1
31 typedef long long ll;
32 const int MAXN = 1e2 + 10;
33 const int INF = 0x3f3f3f3f;
34 const int MOD = 1e9 + 7;
35 char maze[MAXN][MAXN];
36 bool vis[MAXN][MAXN];
37 int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
38 int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
39 int n, m;
40
41 bool judge(int x, int y) {
42 if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] != ‘@‘) return false;
43 return true;
44 }
45
46 void DFS(int x, int y) {
47 for (int i=0; i<8; ++i) {
48 int tx = x + dx[i], ty = y + dy[i];
49 if (!judge (tx, ty)) continue;
50 vis[tx][ty] = true; DFS (tx, ty);
51 }
52 }
53
54 int main(void) { //HDU 1241 Oil Deposits
55 while (scanf ("%d%d", &n, &m) == 2) {
56 if (m == 0) break;
57 for (int i=1; i<=n; ++i) {
58 scanf ("%s", maze[i] + 1);
59 }
60 memset (vis, false, sizeof (vis));
61 int ans = 0;
62 for (int i=1; i<=n; ++i) {
63 for (int j=1; j<=m; ++j) {
64 if (maze[i][j] == ‘@‘ && !vis[i][j]) {
65 ans++; vis[i][j] = true; DFS (i, j);
66 }
67 }
68 }
69 printf ("%d\n", ans);
70 }
71
72 return 0;
73 }
DFS(连通块) HDU 1241 Oil Deposits
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4703148.html