当前位置:网站首页>油田勘探问题
油田勘探问题
2022-06-26 15:45:00 【chengqiuming】
一 原题链接
Oil Deposits - UVA 572 - Virtual Judge
https://vjudge.net/problem/UVA-572
二 输入与输出
1 输入
输入文件包含一个或多个长方形地域。每个地域的第1行都有两个整数 m 和 n,表示地域的行数和列数。如果 m=0,则表示输入结束;否则此后有 m 行,每行都有 n 个字符。每个字符都对应一个正方形区域,字符 * 表示没有油,字符 @ 表示有油。
2 输出
对于每个长方形的地域,都单行输出油藏的个数。
3 输入样例
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
4 输出样例
0
1
2
2
三 算法分析
对这样的油田进行遍历,从每个 @ 格子出发,寻找它周围所有的 @ 格子,同时将这些格子标记一个连通分量号,最后输出连通分量数。使用图的深度优先搜索即可。
例如,针对下面这个用例
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
它的油藏个数就是连通分量的个数,如下图所示。

根据问题描述,水平、垂直或对角线都认为是相邻,因此搜索时,可以从8 个方向进行深度优先搜索,如下图所示。

四 算法设计
1 对字符矩阵中的每个位置都进行判断,如果未标记连通分量号且为 @,则从该位置出发进行深度优先搜索。
2 搜素时需要判断是否出界,是否已有连通分量号或不是 @,否则将该位置标记连通分量为 id,从该位置出发,沿 8 个方向继续进行深度优先搜索。
3 因为有可能包含多个连通分量,因此需要从每个未标记的 @ 进行深度优先搜索。
五 代码
package graph;
import java.util.Scanner;
public class UVA572 {
static int maxn = 100 + 5;
static String str[] = new String[maxn]; // 存储字符矩阵
static int m; // 行
static int n; // 列
static int setid[][];
/**
* 功能描述:深度优先搜索
*
* @param x 行
* @param y 列
* @param id 连通分量号
* @author cakin
* @date 2022/6/26
*/
static void dfs(int x, int y, int id) {
// 出界
if (x < 0 || x >= m || y < 0 || y >= n) {
return;
}
// 已有连通分量号或不是'@'
if (setid[x][y] > 0 || str[x].charAt(y) != '@') {
return;
}
setid[x][y] = id;
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++) {
if (dx != 0 || dy != 0)
// 八个方向深搜
dfs(x + dx, y + dy, id);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
m = scanner.nextInt();
n = scanner.nextInt();
if (m == 0 && n == 0) {
return;
}
for (int i = 0; i <= m - 1; i++) {
str[i] = scanner.next();
}
// 初始化连通分量
setid = new int[maxn][maxn];
int cnt = 0;
for (int i = 0; i <= m - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
if (setid[i][j] == 0 && str[i].charAt(j) == '@')
dfs(i, j, ++cnt);
}
}
System.out.println(cnt);
}
}
}六 测试

边栏推荐
- Auto Sharding Policy will apply Data Sharding policy as it failed to apply file Sharding Policy
- When a project with cmake is cross compiled to a link, an error cannot be found So dynamic library file
- Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
- Handwritten numeral recognition, run your own picture with the saved model
- Super double efficiency! Pycharm ten tips
- 为什么图像分割任务中经常用到编码器和解码器结构?
- Analyse panoramique de la chaîne industrielle en amont, en aval et en aval de la NFT « Dry goods»
- 2 three modeling methods
- 『C语言』题集 of ⑩
- NFT contract basic knowledge explanation
猜你喜欢

Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT

Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system

HW safety response

PCIe Capabilities List

PCIe Capabilities List

2 三种建模方式

Canvas three dot flashing animation

STEPN 新手入門及進階

STEPN 新手入门及进阶

How to create your own NFT (polygon) on opensea
随机推荐
NFT transaction principle analysis (1)
Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
How to create your own NFT (polygon) on opensea
手机上怎么开户?在线开户安全么?
效率超级加倍!pycharm十个小技巧就是这么神
Interview pit summary I
Golang temporary object pool optimization
5000 word analysis: the way of container security attack and defense in actual combat scenarios
NFT 平台安全指南(2)
人人都当科学家之免Gas体验mint爱死机
【leetcode】331. 验证二叉树的前序序列化
svg环绕地球动画js特效
【leetcode】112. 路径总和 - 113. 路径总和 II
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
JS simple deepcopy (Introduction recursion)
现在券商的优惠开户政策是什么?现在在线开户安全么?
STEPN 新手入门及进阶
Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
Summary of data interface API used in word search and translation applications
I want to know how to open an account through online stock? Is online account opening safe?