当前位置:网站首页>Oilfield exploration problems
Oilfield exploration problems
2022-06-26 16:03:00 【chengqiuming】
One Original link
Oil Deposits - UVA 572 - Virtual Judge
https://vjudge.net/problem/UVA-572
Two Input and output
1 Input
The input file contains one or more rectangular regions . The second in each region 1 Each row has two integers m and n, Indicates the number of rows and columns in the region . If m=0, Indicates the end of input ; Otherwise, there will be m That's ok , Every line has n Characters . Each character corresponds to a square area , character * Indicates no oil , character @ It means there is oil .
2 Output
For each rectangular region , Each row outputs the number of reservoirs .
3 sample input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
4 sample output
0
1
2
2
3、 ... and Algorithm analysis
To traverse such oil fields , From each @ Grid departure , Look for all around it @ lattice , At the same time, these grids are marked with a connected component number , Finally, the number of connected components is output . Use the depth first search of the graph .
for example , For the following use case
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
The number of reservoirs is the number of connected components , As shown in the figure below .

According to the problem description , level 、 Both vertical and diagonal lines are considered adjacent , So when searching , It can be downloaded from 8 Depth first search in four directions , As shown in the figure below .

Four Algorithm design
1 Judge each position in the character matrix , If the connected component number is not marked and is @, Then the depth first search is performed from this position .
2 When searching for elements, you need to judge whether they are out of bounds , Whether there is a connected component number or not @, Otherwise, mark the position as the connected component id, From this position , Along the 8 Continue depth first search in all directions .
3 Because it may contain multiple connected components , So you need to start from each unmarked @ Depth first search .
5、 ... and Code
package graph;
import java.util.Scanner;
public class UVA572 {
static int maxn = 100 + 5;
static String str[] = new String[maxn]; // Store character matrix
static int m; // That's ok
static int n; // Column
static int setid[][];
/**
* Function description : Depth-first search
*
* @param x That's ok
* @param y Column
* @param id Connected component number
* @author cakin
* @date 2022/6/26
*/
static void dfs(int x, int y, int id) {
// out
if (x < 0 || x >= m || y < 0 || y >= n) {
return;
}
// There are connected component numbers or not '@'
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)
// Search deeply in eight directions
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();
}
// Initialize connected components
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);
}
}
}6、 ... and test

边栏推荐
- 5000 word analysis: the way of container security attack and defense in actual combat scenarios
- 2 three modeling methods
- 全面解析Discord安全问题
- (1) Keras handwritten numeral recognition and recognition of self written numbers
- 3. Keras version model training
- NFT transaction principle analysis (2)
- 手写数字体识别,用保存的模型跑自己的图片
- How do I open an account on my mobile phone? Is online account opening safe?
- How to create your own NFT (polygon) on opensea
- 3 keras版本模型训练
猜你喜欢

(一)keras手写数字体识别并识别自己写的数字

Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl

Comprehensive analysis of discord security issues

svg canvas画布拖拽

11 cnn简介

Svg savage animation code

Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research

5000字解析:实战化场景下的容器安全攻防之道

如何辨别合约问题

Stepn débutant et avancé
随机推荐
Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
JS creative icon navigation menu switch background color
Reflection modification final
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
TweenMax+SVG切换颜色动画场景
【leetcode】112. Path sum - 113 Path sum II
Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
『C语言』题集 of ⑩
svg环绕地球动画js特效
Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research
11 cnn简介
SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
JVM笔记
今年高考英语AI得分134,复旦武大校友这项研究有点意思
3 keras版本模型训练
IntelliJ idea -- Method for formatting SQL files
Audio and video learning (II) -- frame rate, code stream and resolution
LeetCode 单周赛298,前三题
How to configure and use the new single line lidar
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system