Oil Deposits
Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
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.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
【题目】
勘探油田
Time Limit:1000MS Memory Limit:32768K
【分析】
对这样的油田进行遍历,从每个”@“格子出发,并且寻找它周围所有的”@“格子同时将这些格子做上标记,表明我们已经遍历过该”@“格子,在标记的过程中,我们就可以统计连通块的个数。使用图的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); } }
用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; } } }