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可以访问一个连通块,最后统计数量. / ...
随机推荐
- TCP协议为什么会采用三次握手,若采用二次握手可以吗?
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的.基于IP的传输层协议,采用三次握手确认建立一个连接. TCP为了保证报文传输的 ...
- or1200中载入存储类指令说明
下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 OR1200中实现的载入存储类指令有8条,每条指令的作用与说明如表9.1所看到的. watermark/2/text/aHR0cDo ...
- 原创Oracle数据泵导出/导入(expdp/impdp)
//创建目录 create Or Replace directory dpdata1 as 'd:\test\dump'; //赋予读写权限 grant read,write on directory ...
- JAVA虚拟机内存架构
Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...
- 对TCP性能的考虑
#xiaodeng #对TCP性能的考虑 #HTTP权威指南 86 #对TCP性能的考虑 #HTTP紧挨着TCP,位于其上层.所以HTTP事务的性能很大程度上取决于底层tcp通道的性能. #4.2.1 ...
- assert语句(assert用来判断语句的真假)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python学习手册 868 #assert语句(assert用来判断语句的真假) #案例 mylist=[ ...
- 【转】ubuntu如何修改/添加/删除硬盘分区的挂载(点)?
我(macroliu)的问题:安装ubuntu时,/home单独挂载在一个分区,此后我想调整分区大小,删除了/home对应的分区以及另外几个分区,导致开机时找不到挂载点.把硬盘空闲空间分好区后,想把1 ...
- leetcode719:直线上的第k近点对
问题描述 给定数组a[N],可以确定C(N,2)个点对,也就确定了C(N,2)个距离,求这些距离中第k小的距离(k<C(N,2)). 思路 看到第k小.第k大这种问题,首先想到二分法. 把求值问 ...
- Oralce进程信息查看,Oracle的锁表与解锁
参考: oracle查看锁表进程,杀掉锁表进程 Oracle的锁表与解锁 查看锁表进程SQL语句: select * from v$session t1, v$locked_object t2 whe ...
- Linux内核(17) - 高效学习Linux驱动开发
这本<Linux内核修炼之道>已经开卖(网上的链接为: 卓越.当当.china-pub ),虽然是严肃文学,但为了保证流畅性,大部分文字我还都是斟词灼句,反复的念几遍才写上去的,尽量考虑到 ...