油田(Oil Deposits)
来源:互联网 发布:优化网站的文件和资源 编辑:程序博客网 时间:2024/04/25 20:30
Oil Deposits
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 containingm and n, the number of rows and columns in the grid, separated by a single space. Ifm = 0 it signals the end of the input; otherwise and. Following this arem 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
【题目】
勘探油田
Time Limit:1000MS Memory Limit:32768K
Description:
某石油勘探公司正在按计划勘探地下油田资源,工作在一片长方形的地域中。他们首先将该地域划分为许多小正方形区域,然后使用探测设备分别探测每一块小正方形区域内是否有油。若在一块小正方形区域中探测到有油,则标记为’@’,否则标记为’*’。如果两个相邻区域都为1,那么它们同属于一个石油带,一个石油带可能包含很多小正方形区域,而你的任务是要确定在一片长方形地域中有多少个石油带。所谓相邻,是指两个小正方形区域上下、左右、左上右下或左下右上同为’@’。Input:
输入数据将包含一些长方形地域数据,每个地域数据的第一行有两个正整数m和n,表示该地域由m*n个小正方形所组成,如果m为0,表示所有输入到此结束;否则,后面m(1≤m≤100)行数据,每行有n(1≤n≤100)个字符,每个字符为’@’或’*’,表示有油或无油。每个长方形地域中,’@’的个数不会超过100。Output:
每个长方形地域,输出油带的个数,每个油带值占独立的一行。油带值不会超过100。【分析】
对这样的油田进行遍历,从每个”@“格子出发,并且寻找它周围所有的”@“格子同时将这些格子做上标记,表明我们已经遍历过该”@“格子,在标记的过程中,我们就可以统计连通块的个数。使用图的DFS遍历。
用C++语言编写程序,代码如下:
#include<iostream>#include<cstring>using namespace std;int m, n;const int maxn = 100 + 5;char pic[maxn][maxn];int idx[maxn][maxn];void dfs(int r, int c, int id) {if (r < 0 || r >= m || c < 0 || c >= n) return;//“出界”的格子if (idx[r][c] > 0 || pic[r][c] != '@') return;//不是“@”或者已访问过的格子idx[r][c] = id;//连通分量编号for (int i = -1; i <= 1; i++)for (int j = -1; j <= 1; j++)if (i != 0 || j != 0) dfs(r + i, c + j, id);}int main() {while (cin >> m >> n) {if (m == 0)break;for (int i = 0; i < m; i++) cin >> pic[i];memset(idx, 0, sizeof(idx));int cnt = 0;for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)if (idx[i][j] == 0 && pic[i][j] == '@')dfs(i, j, ++cnt);cout << cnt << endl;}return 0;}
用java语言编写程序,代码如下:
import java.io.BufferedInputStream;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(new BufferedInputStream(System.in));int m, n;while(input.hasNext()) {m = input.nextInt();n = input.nextInt();if(m == 0) break;char[][] pic = new char[m][n];for(int i = 0; i < m; i++)pic[i] = input.next().toCharArray();int cnt = 0;int[][] idx = new int[m][n];for(int i = 0; i < m; i++)for(int j = 0; j < n; j++)if(idx[i][j] == 0 && pic[i][j] == '@')dfs(i, j, ++cnt, m, n, pic, idx);System.out.println(cnt);}}public static void dfs(int r, int c, int id, int m, int n, char[][] pic, int[][] idx) {if(r < 0 || r >= m || c < 0 || c >= n) return;if(idx[r][c] != 0 || pic[r][c] != '@') return;idx[r][c] = id;for(int i = -1; i <= 1; i++)for(int j = -1; j <= 1; j++)if(i != 0 || j != 0)dfs(r + i, c + j, id, m, n, pic, idx);}}
此外,这里还有用图的BFS遍历方法来求解,该BFS遍历使用队列来实现。
用java语言编写程序,代码如下:
import java.io.BufferedInputStream;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(new BufferedInputStream(System.in));int m, n;while(input.hasNext()) {m = input.nextInt();n = input.nextInt();if(m == 0) break;char[][] pic = new char[m][n];for(int i = 0; i < m; i++)pic[i] = input.next().toCharArray();int[][] idx = new int[m][n];int cnt = 0;for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(idx[i][j] == 0 && pic[i][j] == '@') {cnt++;Queue<OilPos> q = new LinkedList<OilPos>();q.add(new OilPos(i, j));while(!q.isEmpty()) {OilPos op = q.poll();idx[op.r][op.c] = cnt;for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) {int newR = op.r + dr;int newC = op.c + dc;if(newR >= 0 && newR < m && newC >= 0 && newC < n&& pic[newR][newC] == '@' && idx[newR][newC] == 0)q.add(new OilPos(newR, newC));}}}}}System.out.println(cnt);}}static class OilPos {int r, c;public OilPos(int r, int c) {this.r = r;this.c = c;}}}
- 油田(Oil Deposits)
- HDU-Oil Deposits-油田搜索
- [UVa]572 油田 Oil Deposits
- ZOJ 1709 Oil Deposits(油田问题DFS)
- Oil Deposits——在图中找油田
- 例题6-12 UVA Oil Deposits油田
- 【HDU-1241】Oil Deposits油田(dfs)
- HDU 1241 Oil Deposits 油田 搜索
- Oil Deposits找联通的油田
- 油田(Oil Deposits,UVa 572)
- Vjudge Oil Deposits 油田问题(dfs)
- 杭电bfs 水题1241 Oil Deposits - 油田 详解 + 分析
- uva 572 Oil Deposits(油田)—— DFS
- HDU 1241 Oil Deposits油田问题(DFS)
- 油田(uva572 Oil Deposits) dfs模板题
- DFS入门精讲:油田(Oil Deposits UVA572)
- 例题6-12 油田(Oil Deposits, UVa 572)
- Uva 572 Oil Deposits 油田 (DFS求连通块)
- cracked me.1. Acid burn
- 菜鸟练习PAT(四)
- 0、0和数值“零”在指针上下文中不是一回事
- 创建和删除快捷方式以及判断是否有快捷方式
- C# 消除游戏
- 油田(Oil Deposits)
- It seems to be a fair amount of MFC
- 高斯模糊及实现
- Jquery----简单的选择
- 【NOIP2012】开车旅行
- 3518e运行HiIspTool.sh提示Isp init failed!
- 指针用操作符’*’和’->’,引用使用操作符’.’
- Android studio 下 配置AndroidAnnotation 环境
- 4.非必要不提供default constructor