LightOJ 1012.Guilty Prince-DFS
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的更多相关文章
- Lightoj 1012 - Guilty Prince
bfs遍历一遍就行了. /* *********************************************** Author :guanjun Created Time :2016/6/ ...
- light 1012 Guilty Prince
题意:一共有 T 组测试数据,每组先给两个数,w,h,表示给一个 高h,宽w的矩阵,‘#’表示不能走,‘.’表示能走,‘@’表示起始点,问,从起始点出发能访问多少个点. 简单的BFS题,以前做过一次. ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- Guilty Prince LightOJ - 1012
Guilty Prince LightOJ - 1012 #include<cstdio> #include<cstring> ][]; int ans,h,w,T,TT; ] ...
- lightoj 1012
水题,dfs #include<cstdio> #include<string> #include<cstring> #include<iostream> ...
- LightOJ——1012Guilty Prince(连通块并查集)
1012 - Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- LightOJ1012-Guilty Prince-DFS
Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. He had ...
- Docker中搭建Hadoop-2.6单机伪分布式集群
1 获取一个简单的Docker系统镜像,并建立一个容器. 1.1 这里我选择下载CentOS镜像 docker pull centos 1.2 通过docker tag命令将下载的CentOS镜像名称 ...
随机推荐
- 批处理之SET命令
除了 下面分别介绍: 表示第二个字符到倒数第三个字符的值
- Struts1防止表单重复提交
package org.zln.struts.action; import org.apache.struts.action.Action; import org.apache.struts.acti ...
- 【题解】Bzoj4316小C的独立集
决定要开始学习圆方树 & 仙人掌相关姿势.加油~~ 其实感觉仙人掌本质上还是一棵树,长得也还挺优美的.很多的想法都可以往树的方面上靠,再针对仙人掌的特性做出改进.这题首先如果是在树上的话那么实 ...
- BZOJ4012 [HNOI2015]开店 【动态点分治 + splay】
题目链接 BZOJ4012 题解 Mychael并没有A掉,而是T掉了 讲讲主要思路 在点分树上每个点开两棵\(splay\), 平衡树\(A\)维护子树中各年龄到根的距离 平衡树\(B\)维护子树中 ...
- cookie 是存储于访问者的计算机中的变量
今天把javascript如何用来创建及存储cookie复习了一下,其中的一点体会拿出来和大家讨论,首先看一下基础知识: 什么是cookie cookie 是存储于访问者的计算机中的变量.每当同一台计 ...
- HttpClient测试类请求端和服务端即可能出现乱码的解决
junit HttpClient 请求端 代码: package com.taotao.httpclient; import java.util.ArrayList; import java.util ...
- HAOI2006 均分数据 [模拟退火]
题目描述 已知N个正整数:A1.A2.--.An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: 输入输出格式 输入格式: 输入文件data.in包括: 第一行 ...
- HDU 多校对抗赛第二场 1010 Swaps and Inversions
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- DES 加密解密
[概念] 数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的.通常 ...
- 在Debian9安装node和npm
这学期又快结束了,坐在每天面对的电脑面,本着整理资料.更换心情的目的,我重装了一下自己的debian.下面就将自己安装node的过程进行记录与分享. node的官网:https://nodejs.or ...