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. 【转】Ubuntu安装ARM架构GCC工具链(ubuntu install ARM toolchain)最简单办法

    原文网址:http://www.cnblogs.com/muyun/p/3370996.html 一.安装ARM-Linux-GCC工具链 只需要一句命令: sudo apt-get install ...

  2. bzoj1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...

  3. Best Time to Buy and Sell Stock III 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...

  4. HDOJ2553-N皇后问题(DFS)

      N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. GridFS

    GridFS是一个建立在MongoDB文档基础之上的轻量级的文件存储规范. GridFS的一个基本思想就是可以将一个大文件分成很多块.每块作为一个单独的文档存储. GridFS支持在文档中存储二进制数 ...

  6. PCRE的安装及使用

    摘自http://www.cnblogs.com/renhao/archive/2011/08/17/2143264.html PCRE的安装及使用 1.主页地址:http://www.pcre.or ...

  7. 并行任务task

    http://msdn.microsoft.com/zh-cn/library/dd537609(v=vs.110).aspx http://www.cnblogs.com/yangecnu/p/So ...

  8. linux下挂载iso镜像文件(转)

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o optio ...

  9. FFmpeg详解

    认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...

  10. mysql 服务不见了的解决办法

    昨天打开电脑mysql突然连接不了了,去服务里找,却找不到mysql服务了 解决:5.0版本:开始->运行->cmd,进到mysql安装的bin目录D:\MySQL\bin>mysq ...