Oil Deposits(DFS,基础题)
来源:互联网 发布:深入java虚拟机 pdf 编辑:程序博客网 时间:2024/05/20 17:26
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5776 Accepted Submission(s): 3360
原题链接:点击打开链接
Total Submission(s): 5776 Accepted Submission(s): 3360
原题链接:点击打开链接
Problem 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 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
0122
Source
Mid-Central USA 1997
Recommend
Eddy
开始时想用并查集。。。后来还是深搜算了,程序很快写出来,可是测试例子的时候却发现有误,debug半天才发现原来是复制网站上的例子的时候换行符不知道为何没复制过去。。。
AC CODE
//Memory: 344 KB Time: 0 MS//Language: C++ Result: Accepted#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#define LL long long#define MAXI 2147483647#define MAXL 9223372036854775807#define dg(i) cout << "*" << i << endl;using namespace std;typedef struct Node{ int x, y; Node(int i, int j) { x = j; y = i; }}P;queue<Node> Q;int n, m;char grid[101][101];int move[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};void DFS(int i, int j){ int ii, jj, k; for(k = 0; k < 8; k++) { ii = i + move[k][0]; jj = j + move[k][1]; if(ii > -1 && jj > -1 && ii < m && jj < n && grid[ii][jj] == '@') { grid[ii][jj] = '*'; DFS(ii, jj); } }}int main(){ int i, j, cnt; char c; while(scanf("%d %d", &m, &n) && m) { cnt = 0; for(i = 0; i < m; i++) { scanf("%c", &c); //吃掉换行符 for(j = 0; j < n; j++) { scanf("%c", &grid[i][j]); if(grid[i][j] == '@') { P t(i, j); Q.push(t); } } } while(!Q.empty()) { P t = Q.front(); Q.pop(); if(grid[t.y][t.x] == '@') { cnt++; DFS(t.y, t.x); } } printf("%d\n", cnt); } return 0;}
- Oil Deposits(DFS,基础题)
- Oil Deposits(基础dfs)
- Oil Deposits(DFS)
- Oil Deposits (DFS)
- Oil Deposits(DFS模板题)
- poj1562--Oil Deposits(DFS)
- HDU1241 Oil Deposits(DFS)
- hdu1241 Oil Deposits(dfs)
- Oil Deposits(dfs)HDU
- Oil Deposits(DFS深搜)
- Oil Deposits(dfs水题)
- (DFS)Oil Deposits--HDOJ
- HDU 1241 Oil Deposits(DFS模板题)
- 油田(uva572 Oil Deposits) dfs模板题
- HDOJ1241 Oil Deposits(DFS模板题)
- UVA572(Oil Deposits)(DFS)
- UVA 572 - Oil Deposits(DFS)
- 13.04.07 Oil Deposits (DFS)
- 不让感情占据那短暂的青春
- C++中类似C# region的功能
- 雷佳歌声动我
- 自动生成Makefile
- startx启动分析
- Oil Deposits(DFS,基础题)
- java 中的 成员变量、局部变量、静态变量、类变量、非静态变量、实例变量、向前引用、非法向前引用、静态代码块、非静态代码块 执行时机
- 遍历XML,在指定位置插入节点
- Hibernate之 对象/关系数据库映射基础(Basic O/R Mapping)
- ORACLE 操作的常用语句,怕忘记了
- 使用模版类作为模版参数
- 翻译:Linux的电源管理架构
- 二维数组中的查找
- 关于开发板挂接时Protocol not support的问题解决