7.12 [NWPU][2014][TRN][3]搜索 POJ 1562 H - 深搜/广搜 基础
2014-07-14 16:33
323 查看
H - 深搜/广搜 基础
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
1562
Description
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 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
are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
Sample Output
从有'@'的地方开始dfs,并将走过的标记为'*',遍历八个方向
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
1562
Description
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 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
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,并将走过的标记为'*',遍历八个方向
#include <iostream> using namespace std; const int MAXN = 101; char field[MAXN][MAXN]; int m, n; void dfs(int x, int y) { field[x][y] = '*'; for(int dx = -1; dx <= 1; dx++) { for(int dy = -1; dy <= 1; dy++) { int nx = x + dx, ny = y + dy; if(0 <= nx && nx < m && 0 <= ny && ny < n && field[nx][ny] == '@') { dfs(nx, ny); } } } } int main() { while(cin >> m >> n && m != 0) { // char field[m] ; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { cin >> field[i][j]; } } int res = 0; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(field[i][j] == '@') { dfs(i, j); res++; } } } cout << res << endl; } return 0; }
相关文章推荐
- 7.12 [NWPU][2014][TRN][4]搜索 C - 广搜 基础
- 7-12 [NWPU][2014][TRN][3]搜索 D - 广搜 基础 POJ 1915
- [NWPU][2014][TRN][17]最小生成树 B - 基础 POJ 2421
- [NWPU][2014][TRN][13]线段树第一讲 A - 基础 POJ 2352
- [NWPU][2014][TRN][13]线段树第一讲 B - 基础 POJ 3264
- [NWPU][2014][TRN][5]二分和贪心 M - 贪心 基础 POJ 2709
- [NWPU][2014][TRN][3]搜索 总结
- [NWPU][2014][TRN][16]图论拓扑排序 H - 基础
- [NWPU][2014][TRN][22]RMQ和LCA E - LCA POJ 1470
- [NWPU][2014][TRN][16]图论拓扑排序 E - 基础 HDU 1285
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- [NWPU][2014][TRN][18]最短路问题 A - 模板 POJ 2387
- [NWPU][2014][TRN][21]数论入门 B - 扩展欧几里得 POJ 1061
- hdu 1241 || poj 1562 Oil Deposits(搜索:BFS水题)
- POJ 1979 基础搜索 DFS\BFS 一
- poj 1562 简单的BFS搜索
- poj 2386 DFS搜索基础
- 7.12 B - 广搜/深搜 基础 POJ 1979
- 7.12 POJ 3278 A - 广搜 基础
- poj 1562 搜索