hud 1241 Oil Deposits
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11842 Accepted Submission(s): 6873
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.
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.
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
0
1
2
2
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char date[110][110];
bool map[110][110];
int n,m;
int dfs(int a,int b)
{
if(map[a][b]==0) return 0;
else {
map[a][b]=0;
if(a>0){
if(b>0) dfs(a-1,b-1);
if(b<n-1) {
dfs(a-1,b+1);
}
dfs(a-1,b);
}
if(a<m-1)
{
if(b>0) dfs(a+1,b-1);
if(b<n-1)dfs(a+1,b+1);
dfs(a+1,b);
}
if(b>0) dfs(a,b-1);
if(b<n-1) dfs(a,b+1);
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
//printf("%d %d\n",m,n);
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++) cin>>date[i];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++)
{
map[i][j]=date[i][j]=='@'?true:false;
}//puts("");
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
bfs版
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[110][110];
int n,m;
queue<pair<int,int> > q;
int bfs(int a,int b)
{
if(map[a][b]=='*') return 0;
else {
q.push(make_pair(a,b));
while (!q.empty())
{
int x,y;
x=q.front().first;
y=q.front().second;
if(x>0){
if(y>0) if(map[x-1][y-1]=='@'){
map[x-1][y-1]='*';
q.push(make_pair(x-1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x-1][y+1]=='@')
{
map[x-1][y+1]='*';
q.push(make_pair(x-1,y+1));
}//bfs(a-1,b+1);
if(map[x-1][y]=='@')
{
map[x-1][y]='*';
q.push(make_pair(x-1,y));
}//bfs(a-1,b);
}
if(x<m-1)
{
if(y>0) if(map[x+1][y-1]=='@'){
map[x+1][y-1]='*';
q.push(make_pair(x+1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x+1][y+1]=='@')
{
map[x+1][y+1]='*';
q.push(make_pair(x+1,y+1));
}//bfs(a-1,b+1);
if(map[x+1][y]=='@')
{
map[x+1][y]='*';
q.push(make_pair(x+1,y));
}//bfs(a-1,b);
}
if(y>0)
if(map[x][y-1]=='@')
{
map[x][y-1]='*';
q.push(make_pair(x,y-1));
}//bfs(a,b-1);
if(y<n-1)
if(map[x][y+1]=='@')
{
map[x][y+1]='*';
q.push(make_pair(x,y+1));
}//bfs(a,b+1);
q.pop();
}
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++){ cin>>map[i];}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
hud 1241 Oil Deposits的更多相关文章
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 杭电1241 Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- [jobdu]丑数
由于思维的惯性,用了queue.后来发现一要注意要用集合判重,二是每次往queue里放的多,后来溢出了,要用long long.但这样要用数组,集合,队列,内存多.效率是O(n*logn)的. #in ...
- 推荐GitHub上10 个开源深度学习框架
推荐GitHub上10 个开源深度学习框架 日前,Google 开源了 TensorFlow(GitHub),此举在深度学习领域影响巨大,因为 Google 在人工智能领域的研发成绩斐然,有着雄厚 ...
- HTML5实现在线抓拍
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- C# word开发
c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中, ...
- Go Hello World!
有些事应该坚持去做 当你半途而废的时候意味着你又要重新开始.那么 Golang Hello world! Java Android 新手 学习 Golang First Day ! go 语言下载: ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.5
Show that matrices with distinct eigenvalues are dense in the space of all $n\times n$ matrices. (Us ...
- android手机屏幕分辨率 及 sp dip(dp) px 区别 及高中低分辨率时处理
分辨率,是指单位长度内包含的像素点的数量,它的单位通常为像素/英寸(ppi).以分辨率为1024×768的屏幕来说,即每一条水平线上包含有1024个像素点,共有768条线,即扫描列数为1024列,行数 ...
- POJ ---3070 (矩阵乘法求Fibonacci 数列)
Fibonacci Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- EF架构随心所欲打造属于你自己的DbModel【转】
前言 我们都知道EF可以生成Dbmodel,系统生成的Model有时候并不是我们想要的,如何我们要生成自己的Model,那么久需要我们手动的去修改T4模版,T4是对“Text Template Tra ...
- [洛谷2397]yyy loves Maths VI
题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居然也不会,所以只好找你 题目描述 他让redbag找众数他还特意 ...