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 ...
随机推荐
- CEF中文教程(google chrome浏览器控件) -- Windows下编译Chromium
CEF中文教程(google chrome浏览器控件) -- CEF简介 2013-04-10 16:48 42928人阅读 评论(4) 收藏 举报 分类: CEF(2) 目录(?)[+] ...
- Python常用模块 (2) (loging、configparser、json、pickle、subprocess)
logging 简单应用 将日志打印到屏幕 import logging logging.debug('debug message') logging.info('info message') log ...
- IEEE发布2016年度编程语言排行榜
C 语言占据榜首,但大数据类是最大赢家. IEEE Spectrum 的第三次"最受欢迎编程语言"交互式排行榜新鲜出炉.因为不可能顾及到每一个程序员的想法,Spectrum 使用多 ...
- ZOJ问题(坑死了)
ZOJ问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Eclipse SVN插件的帐号、password改动
问题描写叙述: Eclipse的SVN插件Subclipse做得非常好,在svn操作方面提供了非常强大丰富的功能.但到眼下为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,并且一旦用户的 ...
- Flex布局实践
介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇>.我的主要参考资料是Landon S ...
- 关于继承扩展ASP.NET控件(以Textbox为例)
以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展. using System; using System.Collections.Generic; using System.Linq ...
- javascript高级知识点——memoization
memoization是一种非常有用的优化技术,它缓存特定输入产生的相应结果.这样麻烦的查找和迭代计算可以尽可能的减少. 它基本的思想是针对特定的输入,已经计算过的结果都是通过缓存当中的数据直接返回而 ...
- SQL语言学习-数据操纵语言
一般而言,数据库中数据的生命周期包括数据插入以及更新.数据删除3个阶段.首先需要用户或者系统将数据插入表.然后,对数据的使用,包括数据的检索以及数据的更新.最后,如果数据已经没有使用价值,则将数据删除 ...
- Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”
原因是 没有设置 /usr/hadoop/tmp 的权限没有设置, 将之改为: chown –R hadoop:hadoop /usr/hadoop/tmp 查看: