油田(Oil Deposits)

 Oil Deposits

Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

Submit  Status

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 and . 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

【题目】


勘探油田
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;
		}
	}
}






你可能感兴趣的:(java,图,uva,油田,oil,Deposits)