1012 - Guilty Prince
Time Limit: 2 second(s) Memory Limit: 32 MB

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.

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 HW 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

以前是无脑DFS水过的,刚看了下大牛的博客自己写了一发过了,原来还有这种做法,知道了连通块的个数和每一个连通块面积的求法。原本以为是用二维的并查集(然而并没写过……),写完发现原来是标号法,学习了。

代码:

#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[n])
return pre[n]=find(pre[n]);
return pre[n];
}
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;
}

LightOJ——1012Guilty Prince(连通块并查集)的更多相关文章

  1. PAT天梯赛练习题——L3-004. 肿瘤诊断(三维连通块并查集)

    L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...

  2. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  3. 牛客练习赛39 D 动态连通块+并查集 X bitset 优化

    https://ac.nowcoder.com/acm/contest/368/D 题意 小T有n个点,每个点可能是黑色的,可能是白色的.小T对这张图的定义了白连通块和黑连通块:白连通块:图中一个点集 ...

  4. [洛谷Luogu]P1141 01迷宫[联通块 并查集]

    题目链接 大致题意 相邻格子不同为连通,计算每个点所在的连通块大小. 想法 我采用了并查集的做法. 开一个辅助数组记录连通块大小,每次合并的时候更新父亲节点的大小即可. 一个点先与它上面的点判定,若判 ...

  5. BZOJ1116:[POI2008]CLO(并查集)

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个t ...

  6. 判断图连通的三种方法——dfs,bfs,并查集

    Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...

  7. poj2513字典树+欧拉图判断+并查集断连通

    题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...

  8. bzoj 4423 [AMPPZ2013]Bytehattan(对偶图,并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4423 [题意] 给定一个平面图,随时删边,并询问删边后两点是否连通.强制在线. [科普 ...

  9. UVA208-Firetruck(并查集+dfs)

    Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Ce ...

随机推荐

  1. ARM指令解析

    今天我来总结一下arm指令的学习,今天我不会对所有的arm指令进行一一的解析,在这里希望大家去看arm汇编手册,这个手册的中文版我放在了http://download.csdn.net/detail/ ...

  2. 【extjs6学习笔记】0.3 准备:系统架构

  3. Highcharts在IE8中不能一次性正常显示的一种解决办法

    由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...

  4. (一)SpringMVC之警告: No mapping found for HTTP request with URI

    这个警告往往是因为url路径不正确. 所以从三个地方下手: 1.springmvc-config.xml中的配置handle,看看是不是因为handle没有配置导致的. 2.如果是使用注解的方式的话, ...

  5. CDN加速静态文件服务器的访问

    1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...

  6. WPF中HyperLink超链接的使用

    HyperLink超链接的简单使用:  XAML里面: <TextBlock> <Hyperlink NavigateUri="http://www.baidu.com&q ...

  7. python 基础之运算符

    运算符 a=10 ,b=20 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...

  8. softmax_loss

    softmax_loss中的ignore_label是来自于loss layer,而不是softmax_loss的参数

  9. 二、pandas入门

    import numpy as np import pandas as pd Series: #创建Series方法1 s1=pd.Series([1,2,3,4]) s1 # 0 1 # 1 2 # ...

  10. Difference between x:Reference and x:Name

    {x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge&qu ...