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

 

LightOJ1012-Guilty Prince-DFS的更多相关文章

  1. Guilty Prince LightOJ - 1012

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

  2. light 1012 Guilty Prince

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

  3. Lightoj 1012 - Guilty Prince

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

  4. LightOJ 1012.Guilty Prince-DFS

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

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

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

  6. LightOJ 1012 简单bfs,水

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

  7. lightoj刷题日记

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

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

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

  9. Lightoj1012【DFS】

    题意: 输出和' @ '相连有多少个' . '包括' @ ',' # '代表墙不能走: 思路: 基础DFS,找到起点,然后跑一下DFS就好了: #include<cstdio> #incl ...

随机推荐

  1. Jstree 使用CheckBox插件 选中父节点时被禁用的子节点也会选中问题

    问题描述: 最近用jstree遇到一个问题,使用CheckBox插件时,当父节点选中时,被禁用的子节点也会选中如下 解决方案: 1.  将jstree升级到最新的版本,v3.3.4及以上就可以 2. ...

  2. magento获取商品的图片

    获取商品的图片主要从catalog_product_entity_media_gallery 表中 该表中各列的属性代表 value_id:记录 ID,可以留空让数据库自动生成. attribute_ ...

  3. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  4. JavaScript的简单入门

    一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...

  5. [数据清洗]- Pandas 清洗“脏”数据(二)

    概要 了解数据 分析数据问题 清洗数据 整合代码 了解数据 在处理任何数据之前,我们的第一任务是理解数据以及数据是干什么用的.我们尝试去理解数据的列/行.记录.数据格式.语义错误.缺失的条目以及错误的 ...

  6. BeanShell断言(一)

    在beanShell中直接可以调用的变量,无需加前缀. 1.log 打印日志 log.info(“在控制台打印日志”); 2.SampleResult 获取SampleResult对象,可以通过这个对 ...

  7. 自动删除文件脚本(Linux shell脚本)

    每天在/home/face/capturepic/2017/目录下都会产生很多文件 /home/face/capturepic/2017/4/21 /home/face/capturepic/2017 ...

  8. 安卓和 java 学习笔记

    1.访问权限为 private 的成员变量或方法,需要执行setAccessible() 方法,并将入口参数设置为 true; 否则不允许访问. 2.为了保证线程的安全,可以使用同步块 synchro ...

  9. Head First设计模式之抽象工厂模式

    一.定义 给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足一下条件:     1)系统中有多个产品族,而系统一次只可能消费其中一族产品.      2)同属于同一个 ...

  10. sql server 错误号大全

    0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒绝访问. 6 句柄无效. 7 存储控制块被损坏. 8 存储空间不足,无法处理此 ...