LightOJ——1012Guilty Prince(连通块并查集)
2016-06-13 22:47
399 查看
1012 - Guilty Prince
Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father's
will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.
For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.
Now write a program to find the number of cells (unit land) he could reach including the cell he was living.
Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are
not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.
1) '.' - land
2) '#' - water
3) '@' - initial position of prince (appears exactly once in a dataset)
以前是无脑DFS水过的,刚看了下大牛的博客自己写了一发过了,原来还有这种做法,知道了连通块的个数和每一个连通块面积的求法。原本以为是用二维的并查集(然而并没写过……),写完发现原来是标号法,学习了。
代码:
Time Limit: 2 second(s) | Memory Limit: 32 MB |
will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.
For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.
Now write a program to find the number of cells (unit land) he could reach including the cell he was living.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are
not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.
1) '.' - land
2) '#' - water
3) '@' - initial position of prince (appears exactly once in a dataset)
Output
For each case, print the case number and the number of cells he can reach from the initial position (including it).Sample Input | Output for Sample Input |
4 6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. | Case 1: 45 Case 2: 59 Case 3: 6 Case 4: 13 |
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> #define INF 0x3f3f3f3f #define MM(x) memset(x,0,sizeof(x)) using namespace std; typedef long long LL; int pos[21][21]; int pre[450],ran[450]; inline void init() { for (int i=0; i<410; i++) { pre[i]=i; ran[i]=1; } } inline int find(int n) { if(n!=pre ) return pre =find(pre ); return pre ; } inline void joint(int a,int b) { int fa=find(a),fb=find(b); if(fa!=fb) { if(ran[fa]>=fb) { ran[fa]+=ran[fb]; pre[fb]=fa; ran[fb]=0; } else { ran[fb]+=ran[fa]; pre[fa]=fb; ran[fa]=0; } } } int main(void) { int tcase,i,j,n,m; int cnt,ori; char C; scanf("%d",&tcase); for (int q=1; q<=tcase; q++) { cnt=0;//给每一个合法的地方标号 MM(pos); init(); scanf("%d%d",&n,&m); for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { cin>>C; if(C!='#') pos[i][j]=++cnt; if(C=='@') ori=cnt; } } for (i=1; i<=m; i++) { for (j=1; j<=n; j++) { if(pos[i][j])//若可到达的地方多了还是用结构体的for比较方便,这里就先用if了 { if(pos[i-1][j]) joint(pos[i-1][j],pos[i][j]); if(pos[i+1][j]) joint(pos[i+1][j],pos[i][j]); if(pos[i][j-1]) joint(pos[i][j-1],pos[i][j]); if(pos[i][j+1]) joint(pos[i][j+1],pos[i][j]); } } } printf("Case %d: %d\n",q,ran[find(ori)]); } return 0; }
相关文章推荐
- [javaSE] GUI(练习-列出指定目录内容)
- LeetCode-232.Implement Queue using Stacks
- break,continue在循环语句中的具体用法
- 静电的ui教程
- 生成随机字符串(UUID方法)
- 调整UILabel行高间距并返回自定义宽高度
- @RequestBody 和@ResponseBody 注解详解
- Top K Frequent Elements
- 从零开始做UI-静电的sketch设计教室 视频教程
- 消息推送业务逻辑处理 “MessageUI”
- 【转】iOS学习之Storyboard中的UIScrollView使用自动布局
- Android UI 之 Tab类型界面总结
- 在Jenkins中使用Git托管项目的源码创建Build Job(托管在GitHub上面)
- BUI之遮罩层
- Java并发编程-并发队列(ConcurrentLinkedQueue)的原理分析
- LeetCode:Unique Paths II
- chain.doFilter(request,response)含义
- 【Leetcode】357. Count Numbers with Unique Digits
- mysql中[Err] 1366 - Incorrect string value: '\xE5\x8D\x问题
- select value from v$diag_info where name='Default Trace File';