The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 ≤ m ≤ 100 and 1 ≤ n ≤ 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either ‘*’, representing the absence of oil, or ‘@’, representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题目类型:简单DFS搜索
算法分析:直接标记每个相连的石油块即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/************************************************** filename :e.cpp author :maksyuki created time :2018/5/31 12:38:37 last modified :2018/5/31 12:49:04 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 166; const int dx[] = {-1, -1, -1, 1, 1, 1, 0, 0}; const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 1}; int row, col, ans; char g[maxn][maxn]; bool vis[maxn][maxn]; bool InLine(int xx, int yy) { if(xx >= 1 && xx <= row && yy >= 1 && yy <= col) return true; return false; } void dfs(int x, int y) { vis[x][y] = true; for(int i = 0; i < 8; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(InLine(xx, yy) && !vis[xx][yy] && g[xx][yy] == '@') dfs(xx, yy); } } int main() { #ifdef LOCAL CFF; //CFO; #endif while(cin >> row >> col) { if(!row && !col) break; memset(vis, false, sizeof(vis)); for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) cin >> g[i][j]; ans = 0; for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) if(g[i][j] == '@' && !vis[i][j]) { dfs(i, j); ans++; } cout << ans << endl; } return 0; } |
- « 上一篇:uva297
- uva1103:下一篇 »