2019-01-28

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.
Sample Input
1 1

3 5
@@*
@
@@*
1 8
@@****@*
5 5
****@
@@@
@@
@@@
@
@@**@
0 0
Sample Output
0
1
2
2
问题链接:https://vjudge.net/contest/279625#problem/G
问题简述:探索地图上有多少个油矿
问题分析:标记探索过的位置
程序说明:dfs
AC通过的C++程序如下:

include <iostream>

include <cstdio>

include <cstring>

include <cmath>

include <algorithm>

include <vector>

include <set>

include <map>

include <queue>

using namespace std;
const int maxn = 105;
int m, n;
int vis[maxn][maxn];
char s[maxn][maxn];
int cnt = 0;
int dir[8][2] = { {0,1},{1,-1},{-1,-1},{-1,0},{0,-1},{-1,1},{1,0},{1,1} };
void dfs(int x, int y) //标记相邻的油田
{
for (int i = 0; i < 8; i++) {
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= m)
continue;
if (!vis[tx][ty] && s[tx][ty] == '@') {
vis[tx][ty] = 1;
dfs(tx, ty);//标记相邻的另一个油田
}
}
return;
}
int main()
{
while (scanf("%d %d", &n, &m) && (n + m))
{
cnt = 0;
memset(vis, 0, sizeof vis);//重置数组
for (int i = 0; i < n; i++)
{
scanf("%s", s[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '@' && !vis[i][j])//查找未被标记的油田
{
cnt++;
vis[i][j] = 1;
dfs(i, j);//标记相邻油田
}
}
}
printf("%d\n", cnt);
}
return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,000评论 0 10
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,391评论 0 3
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 4,157评论 0 2
  • 1.22刘润商学院日课感悟:商业书籍 本章节分享的是建议观看的五本商业书籍,通过这些商业书籍观看其中内容的美妙...
    徐猛_Merlin阅读 285评论 0 0
  • ​本文共2224字。虚构故事,《暴徒》后篇。 我是大山。《暴徒》中的大山。出租车杀人案及抢劫公司工资款的大山。 审...
    不会水的猫阅读 553评论 0 2

友情链接更多精彩内容