hdoj1241 Oil Deposits
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7809 Accepted Submission(s): 4580
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 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.
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.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char s[][];
int dir[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
int n,m;
void dfs(int x,int y)
{ int i,xx,yy;
for(i=;i<;i++)
{
xx=x+dir[i][];yy=y+dir[i][];
if(xx<||xx>n || yy< || yy>m || s[xx][yy]=='*')
continue;
s[xx][yy]='*';
dfs(xx,yy);
}
}
int main()
{
int i,j;
while(cin>>n>>m && m+n)
{ int sum=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
cin>>s[i][j];
for(i=;i<=n;i++)
for(j=;j<=m;j++)
if(s[i][j]=='@')
{
sum++;
// s[i][j]='*';
dfs(i,j);
}
cout<<sum<<endl;
}
return ;
}
解法二:用队列来解
#include<iostream>
#include<queue>
const int MAX=;
int fangxiang[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
using namespace std;
typedef struct dian
{
int x;
int y;
};
char map[MAX][MAX];
int n;
int m;
void BFS(int x,int y)
{
int i,j;
queue<dian>que;
dian in,out;
in.x=x;
in.y=y;
que.push(in);
while(!que.empty())
{
in=que.front();
que.pop();
dian next;
for(i=;i<;i++)
{
next.x=out.x=in.x+fangxiang[i][];
next.y=out.y=in.y+fangxiang[i][];
if(next.x<||next.x>n||next.y<||next.y>m)
continue;
if(map[next.x][next.y]=='@')
{
map[next.x][next.y]='*';
BFS(next.x,next.y);
}
}
}
}
int main()
{
int i,j,sum;
while(cin>>n>>m,m)
{
sum=;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
cin>>map[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(map[i][j]=='@')
{
sum+=;
map[i][j]='*';
BFS(i,j);
}
}
}
cout<<sum<<endl;
}
return ;
}
hdoj1241 Oil Deposits的更多相关文章
- 【伪一周小结(没错我一周就做了这么点微小的工作)】HDOJ-1241 Oil Deposits 初次AC粗糙版对比代码框架重构版
2016 11月最后一周 这一周复习了一下目前大概了解的唯一算法--深度优先搜索算法(DFS).关于各种细节的处理还是极为不熟练,根据题意判断是否还原标记也无法轻松得出结论.不得不说,距离一个准ACM ...
- Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Oil Deposits(dfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- hdu1241 Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) ...
- 杭电1241 Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
随机推荐
- 分析Java Thread State
使用 TDA 工具,看到大量 Java Thread State 的第一反应是: 1,线程状态为“waiting for monitor entry”: 意味着它 在等待进入一个临界区 ,所以它在”E ...
- SSO是什么?
SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信仸的应用系统.它包括可以将这次主要的登录映射到其他应用中用亍同一个用户的登录的机制 ...
- curl命令(测试连接命令)
curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS.f ...
- C# 关闭显示器(显示)
1.先引入DllImport所在的名称空间 using System.Runtime.InteropServices; 2.引入方法 [DllImport("user32.dll" ...
- 每天写点shell脚本 (持续更新)
1.显示系统信息脚本 #!/bin/bash #A system information gathering script #Command UNAME="uname -a" pr ...
- 网址URL中特殊字符转义编码
网址URL中特殊字符转义编码字符 - URL编码值空格 - %20" - %22# - %23% - %25& - %26( - %28) - %29+ - %2B, - %2C/ ...
- 安装mysql 和 apache
一. 安装apache服务器 1. 检查apache服务器是否安装 #service httpd status 2. 如提示未被识别的服务,则表明组件未安装,需手动安装 #yum install ht ...
- vim+xdebug调试PHP
一.安装xdebug 1.编译安装xdebug,也可以使用pecl install xdebug wget http://xdebug.org/files/xdebug-2.3.2.tgz tar - ...
- JNI入门
JNI是Java Native Interface的缩写,Native指C/C++. JNI内容涉及两个方面: Java调用C,这种情况是最主要的 C调用Java,这种情况不常见 第一步:编写Java ...
- 【C++】关于带const的指针问题
区分const出现在*前还是*后 前: 例如const int *p,这种表示情况下,p本身可以改变,即p可以指向不同的地址, 但是p指向的内容不可改变. 就像你喜欢看书,图书馆规定你可以任意借阅及更 ...