当前位置:网站首页>搜索(集训)
搜索(集训)
2022-06-21 16:13:00 【Aidan 347】
Oil Deposits
描述
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.
输入
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.
输出
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.
样例输入
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出
0
1
2
2
思路:DFS每个@的8个方向,之后将搜索到的@换成*表示已经搜索过
#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;
}
边栏推荐
- Volcano engine + Yanrong yrcloudfile, driving new growth of data storage
- 正则表达式
- 透过华为军团看科技之变(四):互动媒体(音乐)
- Is it safe and reliable to open futures accounts on koufu.com?
- Deeply understand the attention mechanism of map
- Let, with, apply, also, run
- BFS and DFS
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- Analysis of 43 cases of MATLAB neural network: Chapter 26 classification of LVQ Neural Network - breast tumor diagnosis
- Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud
猜你喜欢

堆栈认知——堆简介

Xticks function in MATLAB

PTA l3-031 thousand hand Guanyin (30 points)

Runmeide Healthcare a réussi l'audience d'inscription sur la liste: les pertes devraient augmenter, Huo Yunfei Brothers détenant environ 33%

Reids面试题集合 数据结构+穿透雪崩+持久化+内存淘汰策略+数据库双写+哨兵

神经网络七十年:回顾与展望

Jetpack compose phase

Two understandings of Bayes formula

Compose programming idea

What is the process of en 1101 flammability test for curtains?
随机推荐
超分之RLSP
How many items should the indoor intumescent fire retardant coating meet according to BS 476-21 fire resistance standard?
PTA L3-031 千手观音 (30 分)
Bm95 points candy problem
拦截器实现网页用户登陆
Zhong'an insurance, together with Alibaba health and huiyitianxia, explores a new model of Internet chronic disease management
regular expression
shamir
数字藏品系统开发,NFT艺术品交易平台搭建
How to adjust 3DE 3D model view if you can't see it
经纬度转换为距离
为什么RedisCluster设计成16384个槽?
C语言与Lua的交互(实践二)
Go corn timing task simple application
Differences between fragmentstatepageradapter and fragmentpageradapter
BM19 寻找峰值
3DE 运动轮廓数据修改
solidity智能合约面试题
[dataset] |bigdetection
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分