当前位置:网站首页>Search (intensive training)
Search (intensive training)
2022-06-22 04:11:00 【Aidan 347】
Oil Deposits
describe
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 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
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.
The sample input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample output
0
1
2
2
Ideas :DFS Every @ Of 8 A direction , Which will be searched later @ Switch to * Indicates that you have searched
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int N = 110;
int n,m;
char g[N][N];
int res;
void dfs(PII u)
{
for(int i=0;i<8;i++)
{
int xx = u.first+dx[i];
int yy = u.second+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&g[xx][yy]=='@')
{
g[xx][yy]='*';
PII next;
next.first = xx;
next.second = yy;
dfs(next);
}
}
}
int main()
{
while(true)
{
res=0;
cin>>n>>m;
if(m==0&&n==0) break;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>g[i][j];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(g[i][j]=='@')
{
g[i][j] = '*';
res++;
PII point;
point.first = i;
point.second = j;
dfs(point);
}
}
}
cout<<res<<endl;
}
return 0;
}
边栏推荐
- Fastadmin list multi image preview (zoom in preview, rotation) one line of code
- Idea blue screen solution
- WebView error
- Redis和MySQL如何保持数据一致性?强一致性,弱一致性,最终一致性
- Case driven: a detailed guide from getting started to mastering shell programming
- WPF DataContext 使用(2)
- Cordova 项目中自定义插件--插件创建流程
- axios get传参拼接数据库字段
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- read stream 特别注意
猜你喜欢
随机推荐
Is it safe to open an account in Guoyuan futures?
Fluent syntax configuration
LOCAL=NO
【写文章神器】标记语言Markdown的使用
图的DFS
希尔排序
fastadmin列表多图预览(放大预览,轮播)一行代码
【BP回归预测】基于matlab GA优化BP回归预测(含优化前的对比)【含Matlab源码 1901期】
active RM机子断电后,RM HA切换正常。但是YarnUI上查看不到集群资源,application也一直处于ACCEPTED状态。
Binary tree cueing
順序錶的基本操作
PCM data format
Yum command
BFs of figure
SQL operation: with expression and its application
七千字详解阿里云新一代云计算体系架构 CIPU
TCL华星发布全球首款0.016Hz超低频OLED穿戴设备屏
WPF DataContext usage (2)
It is about one-step creating Yum source cache in Linux
Mqtt of NLog custom target








