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. 从Hello, world开始认识IL <第一篇>

    IL代码分析方法 Hello, world历史 .NET学习方法论 1.引言 1988年Brian W.Kernighan和Dennis M.Ritchie合著了软件史上的经典巨著<The C ...

  2. WordPress SEO ☞ WordPress网站终极优化指南

    原文地址:http://www.eastdesign.net/wordpress-seo/ 最新消息,东方设计学院 WordPress SEO 系列视频教程正在持续更新中,目前为了不至于让视频传播过于 ...

  3. UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>

    F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  4. tc令牌桶限速心得

    一.实验拓扑与实验现象 实验拓扑如图所示,在①号机上发送数据,③号机上接受数据,同时在④号机的eth1与eth2网口限制速率为115200kbps,命令如下 tc qdisc add dev eth1 ...

  5. JNI的替代者—使用JNA访问Java外部功能接口

    摘自:http://www.cnblogs.com/lanxuezaipiao/p/3635556.html JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Ja ...

  6. 华为Java笔试题

    华为Java笔试题+数据库题 一. 单项选择题 1.Java是从( )语言改进重新设计. A.Ada B.C++ C.Pasacal D.BASIC 2.下列语句哪一个正确( ) A. Java程序经 ...

  7. Android学习总结——SharedPreferences

    SharePreferences存储方式,只是轻量级数据存储,xml格式的数据显示方式.简单存储步骤如下:一:获取SharePreferences对象1.SharedPreferences pref ...

  8. Virtual Friends(并查集+map)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 树的直径 poj 2631

    树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离 #include <iostream> #include <algorithm> #inc ...

  10. H5页面之iphone6的适配

    兼容iphone各版本机型最佳的方式就是自适应. 1.viewport 简单粗暴的方式: 1 <meta name="viewport" content="widt ...