博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1012——Guilty Prince(DFS)
阅读量:2343 次
发布时间:2019-05-10

本文共 2492 字,大约阅读时间需要 8 分钟。

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
..#.#..
..#.#..
###.###
…@…
###.###
..#.#..
..#.#..
Output for Sample Input
Case 1: 45
Case 2: 59
Case 3: 6
Case 4: 13

水题

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 25#define Mod 10001using namespace std;int n,m,vis[MAXN][MAXN];char mp[MAXN][MAXN];int dfs(int x,int y){ if(mp[x][y]=='#'||x<1||x>n||y<1||y>m||vis[x][y]) return 0; vis[x][y]=1; return 1+dfs(x+1,y)+dfs(x,y+1)+dfs(x-1,y)+dfs(x,y-1);}int main(){ int t; scanf("%d",&t); for(int cas=1; cas<=t; ++cas) { scanf("%d%d",&m,&n); memset(vis,0,sizeof(vis)); int a,b; for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) { cin>>mp[i][j]; if(mp[i][j]=='@') { a=i; b=j; } } /*for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) cout<

转载地址:http://opcvb.baihongyu.com/

你可能感兴趣的文章
费雪耶兹(Fisher–Yates) 也被称作高纳德( Knuth)随机置乱算法
查看>>
C/C++中变量的存储位置
查看>>
linux gdb的详细用法 运行与断点
查看>>
删除vector中重复元素
查看>>
和为s的连续正数序列
查看>>
什么是Redis?什么是nosql?NoSQL数据库的四大分类
查看>>
为什么说Redis是单线程的以及Redis为什么这么快!
查看>>
redis的过期健删除策略以及内存淘汰机制
查看>>
map 如何使用结构体作为自定义键值
查看>>
Mysql几种索引类型的区别及适用情况
查看>>
Redis持久化的两种方式
查看>>
判断一个数组,是否可以分成两个数组之和相等的数组
查看>>
背包问题
查看>>
结构体变量之间的比较和赋值原理
查看>>
C++ const修饰函数、函数参数、函数返回值
查看>>
将单链表的每k个节点之间逆序
查看>>
删除链表中重复的节点——重复节点不保留
查看>>
2018腾讯校招编程题——最重要的城市
查看>>
删除链表中重复的节点——重复节点保留一个
查看>>
实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit()).md
查看>>