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镜像名称 ...
随机推荐
- 一道js的前端面试题,主要弄清楚逗号表达式的作用
群里看到的一道前端JS面试题.我以为我会,其实我错了.找了很多资料,写下来. var i,j,k; for( i = 0 , j = 0; i < 10 , j < 6; i++ , j+ ...
- Socket常见错误代码与描述
最近程序 出现 几次 Socket 错误, 为了便于 差错.. 搜了一些 贴在这里.. 出现网络联机错误Socket error #11001表示您的计算机无法连上服务器,请检查您的Proxy设定以及 ...
- Codeforces Round #390 (Div. 2) E(bitset优化)
题意就是一个给出2个字符矩阵,然后进行匹配,输出每个位置的匹配的结果 (超出的部分循环处理) 一种做法是使用fft,比较难写,所以没有写 这里使用一个暴力的做法,考虑到一共只出现26个字符 所以使用一 ...
- 【题解】NOI2014购票
实际上是一个不完美算法……cogs上面A不掉(爆栈啦).感谢机房大佬PPY的指点,现在也写出来供大家参考参考,理解起来应该是比较简单的一种. 我们首先get出斜率优化方程: \(dp[v] = dis ...
- [SCOI2010]序列操作 线段树
---题面--- 题解: 在考场上打的这道题,出人意料的很快就打完了?! 直接用线段树,维护几个东西: 1,lazy标记 : 表示区间赋值 2,mark标记:表示区间翻转 3,l1:前缀最长连续的1的 ...
- SDOI 2009 学校食堂 状压dp
这个题的关键处1 紧跟着他的bi个人 —— 由此得出任意一个状态都可以表示为 有第一个人没吃到饭做分隔的前面所有人已吃饭,并用1<<8表示之后的(包括他)的八个人的状态2 信息仍然是上一个 ...
- Android tips tool 发现的性能问题(转载翻译)
先翻译刚好在研究到的一段,其余的无限期待续. 1.ObsoleteLayoutParam不起作用的标签 Invalid layout param in a LinearLayout: layout_c ...
- clear:both其实是有瑕疵的
在开发中,从美工MM给你Html代码中,肯定能经常看"<div style="clear:both;"></div>"这样的代码,但是你 ...
- hbase监控实现
目前实现的监控概览
- 使用MAT分析内存泄露
使用MAT分析内存泄露 对于大型服务端应用程序来说,有些内存泄露问题很难在测试阶段发现,此时就需要分析JVM Heap Dump文件来找出问题.随着单机内存越来越大,应用heap也开得越来越大,动辄十 ...