当前位置:网站首页>搜索(集训)
搜索(集训)
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;
}
边栏推荐
- Two understandings of Bayes formula
- List set to comma concatenated string
- list的使用
- Class, interface, function
- 直播平台开发,直播各个分类单例设计展示
- 为什么RedisCluster设计成16384个槽?
- PTA l3-031 thousand hand Guanyin (30 points)
- 火山引擎+焱融 YRCloudFile,驱动数据存储新增长
- Google play application signature key certificate, upload signature certificate difference
- The source code of the online live broadcast system enables you to request the list interface and touch the bottom page to load
猜你喜欢

PTA L3-031 千手观音 (30 分)

Vscade tool

Analysis of 43 cases of MATLAB neural network: Chapter 26 classification of LVQ Neural Network - breast tumor diagnosis

MySQL 1055错误-this is incompatible with sql_mode=only_full_group_by解决方案

Excess rlsp

【技术管理】集结号与亮剑团队

数字藏品系统开发,NFT艺术品交易平台搭建

数字经济王宁教你如何正确地选择短期投资

aws elastic beanstalk入门之简介

Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
随机推荐
Two understandings of Bayes formula
Stm32f1 and stm32subeide programming example - linear Hall effect sensor driver
加速雲原生應用落地,焱融 YRCloudFile 與天翼雲完成兼容性認證
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
BFS与DFS
Yrcloudfile of Yanrong technology has completed the compatibility certification with ANTP to jointly create a new blueprint for storage
Compose programming idea
直播平台开发,直播各个分类单例设计展示
MySQL 1055错误-this is incompatible with sql_mode=only_full_group_by解决方案
【蓝桥杯省赛真题35】Scratch水面倒影 少儿编程scratch编程蓝桥杯省赛真题讲解
Go corn timing task simple application
拦截器实现网页用户登陆
MySQL 1055 error -this is incompatible with SQL_ mode=only_ full_ group_ By solution
3DE 网格坐标点与物体的附加
How to adjust 3DE 3D model view if you can't see it
Xticks function in MATLAB
C language DLL Dynamic Link Library
Bm95 points candy problem
智慧三农数字王宁:适合初学者的前5日交易秘诀
经纬度转换为距离