POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14628 | Accepted: 7972 |
Description
Input
Output
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求联通块)的更多相关文章
- [POJ] 1562 Oil Deposits (DFS)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16655 Accepted: 8917 Des ...
- (简单) POJ 1562 Oil Deposits,BFS。
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
- POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS
现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...
- poj 1562 Oil Deposits (广搜,简单)
题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...
- POJ 1562 Oil Deposits
转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...
- HDU - 1213 dfs求联通块or并查集
思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
- [洛谷P1197/BZOJ1015][JSOI2008]星球大战Starwar - 并查集,离线,联通块
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
随机推荐
- HtmlAttribute HTML属性处理类
HtmlAttribute 在HtmlAgilityPack扮演的是一个HTML代码属性的容器,同时提供了用于处理HTML属性的一些功能. 一.属性 int Line { get; } 获取文档中的此 ...
- 关于在Reshaper中添加代码模板代码段
http://www.cnblogs.com/tristinjet/archive/2009/08/19/1550203.html 去 tools->模板中进行模板编辑设置
- Installing — pylibmc 1.2.3 documentation
Installing - pylibmc 1.2.3 documentation libmemcached
- 重大新闻:借贷宝不用绑卡了,借贷宝APP推出肖像识别新功能!
动动手指,20元人民币立即到手:http://www.cnblogs.com/mfryf/p/4754384.html 滴滴打车烧钱十几个亿,狂送打车券,很多人天天免费坐车! 去年年初百度钱包注册奖励 ...
- 正则表达式、find、grep、awk、sed
1.正则表达式 (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成. (2)基本元字符集及其含义 ^ :只 ...
- Ehcache RIM
Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示: Ehcache支持多种集群方式,下面以RMI通信方式为例,来具 ...
- More is better(并差集)
More is better Time Limit : 5000/1000ms (Java/Other) Memory Limit : 327680/102400K (Java/Other) To ...
- WPF拖动总结[转载]
WPF拖动总结 这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...
- JS给元素循环添加事件的问题
<ul> <li>男</li> <li>女</li> <li>老</li> <li>少</li&g ...
- C/C++ 笔试、面试题目大汇总 转
C/C++ 笔试.面试题目大汇总 这些东西有点烦,有点无聊.如果要去C++面试就看看吧.几年前网上搜索的.刚才看到,就整理一下,里面有些被我改了,感觉之前说的不对或不完善. 1.求下面函数的返回值( ...