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. 软件架构 "4+1" 视图模型

    1995年,Philippe Kruchten在<IEEE Software>上发表了题为<The 4+1 View Model of Architecture>的论文,引起了 ...

  2. bzoj1623 [Usaco2008 Open]Cow Cars 奶牛飞车

    Description   编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).     ...

  3. nginx监听端口和反向代理端口的权限问题

    Linux的SELinux安全性控制除作用于文件系统外还作用于端口,这使得那些作为服务启动的进程只能在规定的几个端口上监听.为叙述方便我们称之为受控端口. nginx监听端口 要查看当前有哪些受控端口 ...

  4. canvas动画文字效果

    Doughnut Chartvar c=document.getElementById("canvas");var ctx=c.getContext("2d") ...

  5. hdu - 4651 - Partition

    题意:把一个整数N(1 <= N <= 100000)拆分不超过N的正整数相加,有多少种拆法. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid ...

  6. Nginx学习之四-Nginx进程同步方式-自旋锁(spinlock)

    自旋锁简介 Nginx框架使用了三种消息传递方式:共享内存.套接字.信号. Nginx主要使用了三种同步方式:原子操作.信号量.文件锁. 基于原子操作,nginx实现了一个自旋锁.自旋锁是一种非睡眠锁 ...

  7. 细说php(三) 运算符与表达式

    一.算术运算符    +   -  *  /  %  ++  -- <?php $a = 100; $a++; // $a=$a+1; 先用变量再自增1 ++$a; // $a=$a+1; 先用 ...

  8. Android系统的开机画面显示过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7691321 好几个月都没有更新过博客了,从今天 ...

  9. 【模拟】【HDU1443】 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  10. Git GUI简易使用教程

    Git GUI简易使用教程:先上图~~~~~ 一. 先从添加文件开始讲(先上图): 1.选中要添加的文件,点击菜单栏的“提交”,选中“缓存为提交”,