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

4

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

...@...

###.###

..#.#..

..#.#..

Sample Output

Case 1: 45

Case 2: 59

Case 3: 6

Case 4: 13

题意好理解,直接改的HDU1312的板子题。。。

代码:

#include<bits/stdc++.h>
using namespace std;
int direct [][]={-,,,,,,,-};
char str[][];
bool flag[][];
int w,h,ans;
void DFS(int x,int y){
for(int i=;i<;i++){
int p=x+direct[i][];
int q=y+direct[i][];
if(p>=&&q>=&&p<h&&q<w&&flag[p][q]==&&str[p][q]=='.'){
ans++;
flag[p][q]=;
DFS(p,q);
}
}
}
int main(){
int i,j,k,t,num=;
int Dx,Dy;
while(~scanf("%d",&t)){
while(t--){
num++;
scanf("%d%d",&w,&h);
if(w==&&h==)break;
memset(flag,,sizeof(flag));
getchar();
for(i=;i<h;i++){
for(j=;j<w;j++){
scanf("%c",&str[i][j]);
if(str[i][j]=='@'){
Dx=i;
Dy=j;
}
} getchar();
}
ans=;
flag[Dx][Dy]=;
DFS(Dx,Dy);
printf("Case %d: %d\n",num,ans);
}
}
return ;
}

 

LightOJ 1012.Guilty Prince-DFS的更多相关文章

  1. Lightoj 1012 - Guilty Prince

    bfs遍历一遍就行了. /* *********************************************** Author :guanjun Created Time :2016/6/ ...

  2. light 1012 Guilty Prince

    题意:一共有 T 组测试数据,每组先给两个数,w,h,表示给一个 高h,宽w的矩阵,‘#’表示不能走,‘.’表示能走,‘@’表示起始点,问,从起始点出发能访问多少个点. 简单的BFS题,以前做过一次. ...

  3. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  4. Guilty Prince LightOJ - 1012

    Guilty Prince LightOJ - 1012 #include<cstdio> #include<cstring> ][]; int ans,h,w,T,TT; ] ...

  5. lightoj 1012

    水题,dfs #include<cstdio> #include<string> #include<cstring> #include<iostream> ...

  6. LightOJ——1012Guilty Prince(连通块并查集)

    1012 - Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. ...

  7. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  8. LightOJ1012-Guilty Prince-DFS

    Guilty Prince  Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. He had ...

  9. Docker中搭建Hadoop-2.6单机伪分布式集群

    1 获取一个简单的Docker系统镜像,并建立一个容器. 1.1 这里我选择下载CentOS镜像 docker pull centos 1.2 通过docker tag命令将下载的CentOS镜像名称 ...

随机推荐

  1. [剑指Offer] 10.矩形覆盖

    题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? [思路]可归纳得出结论: f(n) = f(n-1) + f ...

  2. 【bzoj1999】[Noip2007]Core树网的核 树的直径+双指针法+单调队列

    题目描述 给出一棵树,定义一个点到一条路径的距离为这个点到这条路径上所有点的距离的最小值.求一条长度不超过s的路径,使得所有点到这条路径的距离的最大值最小. 输入 包含n行: 第1行,两个正整数n和s ...

  3. 【bzoj3931】[CQOI2015]网络吞吐量 最短路+最大流

    题目描述 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的地,路由器需要选择最优的路径转发 ...

  4. G D 3 2 预 处 理 符 号 配 置 中 定 义

    Is mainly used in MCU and peripherals to choose, without having to modify macro definitions in the c ...

  5. [USACO08MAR]土地征用Land Acquisition

    题面在这里 题意 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地. 如果约翰单买一块土地,价格就是土地的面积,但他可以选择并购一组土地, 并购的价格为这些土地中最大的长乘以最大的宽. 给定每 ...

  6. 怎么将数据库从Oracle迁移到SQL Server,或从Oracle迁移到MySQL

    有时候我们有迁移数据库的需求,例如从Oracle迁移到SQL Server,或者从MySQL迁移到Oracle. 很多江湖好汉一时不知如何手工操作,所幸的是Navicat提供了迁移的自动化操作界面. ...

  7. C# windows application Hello World

    创建一个Windows application项目,然后可以调用里面的工具来生成代码. using System; using System.Collections.Generic; using Sy ...

  8. jsp小知识点(2)

    一:自定义的函数库:在wh.tld中 <description>JSTL 1.1 functions library</description> <display-nam ...

  9. SpringMVC——如何获取请求参数

    参考 http://www.cnblogs.com/bigdataZJ/p/springmvc2.html (文章讲了几个注解的使用,但不够深入.) 参考 http://www.cnblogs.com ...

  10. python自带的线程池和进程池

    #python自带的线程池 from multiprocessing.pool import ThreadPool #注意ThreadPool不在threading模块下 from multiproc ...