poj 1562 DFS(深度遍历) - 算法
2017-07-17 22:55
316 查看
题目要求
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
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
代码实现
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
代码实现
package com.daxiong.day5; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { int row = in.nextInt(); // 读取行数 int col = in.nextInt(); // 读取列数 if (row == 0 && col == 0) break; int count = 0; // 记录总数 int[][] arr = new int[row + 2][col + 2]; for (int i = 1; i <= row; i++) { String s = in.next(); for (int j = 1; j <= col; j++) { arr[i][j] = s.charAt(j - 1); // 拆分获取字符 } } /* * for (int i = 1; i <= row; i++) { for (int j = 1; j <= col; j++){ * System.out.println("arr[][]" + arr[i][j]); } } */ for (int i = 1; i <= row; i++) for (int j = 1; j <= col; j++) if (arr[i][j] == 64) { find(arr, i, j); count++; } System.out.println(count); } in.close(); } public static void find(int[][] a, int i, int j) { a[i][j] = 65; for (int k = i - 1; k <= i + 1; k++) for (int w = j - 1; w <= j + 1; w++) if (a[k][w] == 64) find(a, k, w); } }
相关文章推荐
- poj 1562 深度优先遍历
- POJ 1683 所谓DFS(深度优先遍历)
- 算法基础——DFS(深度优先遍历)
- 【算法——02】图的遍历——BFS广度优先搜索、DFS深度优先搜索
- DFS深度优先遍历算法简单分析
- 第12周项目3-(1)图遍历算法实现、实现深度优先遍历—DFS
- DFS深度搜索算法实现深度探究解析-以POJ 1040为例
- ACM-POJ 1562 DFS 深度优先搜索
- poj 1154(dfs深度优先遍历)
- 数据结构和算法之图---图的遍历之深度搜索dfs
- DFS深度搜索算法实现深度探究解析-以POJ 1040为例
- 图——图的遍历——深度优先遍历DFS
- 第十二周项目--深度优先遍历——DFS
- 图的遍历之-DFS深度优先遍历C++实现
- poj1562--Oil Deposits(DFS)
- poj 1562 dfs
- DFS(深度优先遍历搜索解析)
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- 继下午的POJ_1644_放苹果的dfs(深度优先搜索)算法解决代码
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS