Oil Deposits
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14628   Accepted: 7972

Description

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 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

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
方法1:并查集
收获:二维数组转一维数组公式:i*m(为列数)+j;
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
#define maxm 100000
int n, m;
char mp[maxn][maxn];
int root[maxm];
bool solve(int x, int y)
{
if(x < || x >= n || y < || y >= m)
return true;
return false;
}
int find_root(int x)
{
if(x != root[x])
root[x] = find_root(root[x]);
return root[x];
}
void uni(int a, int b)
{
int x = find_root(a);
int y = find_root(b);
if(x != y)
{
root[y] = x;
}
}
void judge(int x, int y)
{
for(int i = -; i <= ; i++)
for(int j = -; j <= ; j++)
{
if(i != || j != )
{
int dx = x + i;
int dy = y + j;
if(mp[dx][dy] == '@' && !solve(dx, dy))
{
int p = x * m + y;
int q = dx * m + dy;
uni(p, q);
}
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m) && (n+m) != )
{
memset(root, -, sizeof root);
for(int i = ; i < n; i++)
scanf("%s", mp[i]);
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(mp[i][j] == '@')
root[i*m+j] = i * m +j;
}
} for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
if(mp[i][j] == '@')
{
judge(i, j);
}
}
int ans = ;
for(int i = ; i < n ; i++)
for(int j = ; j < m; j++)
if(root[i*m+j] == i*m+j)
ans++;
printf("%d\n", ans);
}
return ;
}
方法二:
DFS入门题
收获:做搜索的题目调试的时候可以用打印中间路径的方法来调试。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
int n, m;
char mp[maxn][maxn];
int vis[maxn][maxn];
bool solve(int x, int y)
{
if(x < || x >= n || y < || y >= m)
return true;
return false;
}
void dfs(int x, int y)
{
for(int i = -; i <= ; i++)
for(int j = -; j <= ; j++)
{
if(i != || j != )
{
int dx = x + i;
int dy = y + j;
if(!vis[dx][dy] && mp[dx][dy] == '@' && !solve(dx, dy))
{
vis[dx][dy] = ;
dfs(dx, dy);
}
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m) && (n+m) != )
{
for(int i = ; i < n; i++)
scanf("%s", mp[i]);
int cnt = ;
memset(vis, , sizeof vis);
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
if(mp[i][j] == '@' && !vis[i][j])
{
vis[i][j] = ;
++cnt;
dfs(i, j);
}
}
printf("%d\n", cnt);
}
return ;
}

POJ 1562 Oil Deposits (并查集 OR DFS求联通块)的更多相关文章

  1. [POJ] 1562 Oil Deposits (DFS)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Des ...

  2. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  3. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  4. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

  5. poj 1562 Oil Deposits (广搜,简单)

    题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...

  6. POJ 1562 Oil Deposits

    转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...

  7. HDU - 1213 dfs求联通块or并查集

    思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

  8. [洛谷P1197/BZOJ1015][JSOI2008]星球大战Starwar - 并查集,离线,联通块

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  9. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

随机推荐

  1. android gridview布局,实现长按某一个,所有项都显示删除的图标

    最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标. gridview的布局文件如下: <?xm ...

  2. Android ListView实现圆角

    首先呢,我们还是看几个示图: 这种带有圆角的listview' 看起来很棒吧,确实是这样,其实也不能这么说,主要方形太多了,斯通见惯就不值钱了,“物以稀为贵嘛”. 就好比学java都搞androd,很 ...

  3. Roy the Robber

    Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that th ...

  4. cf479E Riding in a Lift

    E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Valid Palindrome 解答

    Question Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...

  6. libcurl 多线程使用注意事项 - Balder~专栏 - 博客频道 - CSDN.NET

    libcurl 多线程使用注意事项 - Balder~专栏 - 博客频道 - CSDN.NET libcurl 多线程使用注意事项 分类: C/C++学习 2012-05-24 18:48 2843人 ...

  7. android的init过程分析

    前言 Android系统是运作在linux kernal上的,因此它的启动过程也遵循linux的启动过程,当linux内核启动之后,运行的第一个进程是init,这个进程是一个守护进程,它的生命周期贯穿 ...

  8. XMind快捷键可以自定义吗

    在使用快捷键的时候,不知你是否有过这样的疑问,为什么这个操作的快捷键一定要是这个呢,我为什么不能换成其他的按键呢.其实这些在XMind思维导图中都是可以更改的,用户可以根据自己的操作习惯来定义快捷键命 ...

  9. sqlite数据库读写在linux下的权限问题

    近期在学linux,恰巧有个php项目要做.于是配置好环境打算在linux下做. 无奈站点执行后一片空白.经过调试发现是sqlite数据库的问题. 安装sqlite扩展 apt-get install ...

  10. C#中如何只保留小数点后面两位?

    string.format("%.4f",1/3) 1.Math.Round(0.333333,2);//按照四舍五入的国际标准2. double dbdata=0.335333; ...