LightOJ
来源:互联网 发布:苹果版解压缩软件 编辑:程序博客网 时间:2024/06/15 20:29
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 starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing two positive integers W andH; W and H are the numbers of cells in thex and y directions, respectively. W andH are not more than 20.
There will be H more lines in the data set, each of which includesW 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)
For each case, print the case number and the number of cells he can reach from the initial position (including it).
4
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
Case 1: 45
Case 2: 59
Case 3: 6
Case 4: 13
这题就是一个简单的dfs,以@为起点(@算在内),数数能到达的.的个数#为障碍物
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char map[99][99];int v[99][99];int n, m;int sum;void dfs(int x, int y){ v[x][y] = 1; sum++; if(x+1 < n && map[x+1][y] != '#' && !v[x+1][y]) dfs(x+1,y); if(x-1 >= 0 && map[x-1][y] != '#' && !v[x-1][y]) dfs(x-1,y); if(y+1 < m && map[x][y+1] != '#' && !v[x][y+1]) dfs(x,y+1); if(y-1 >= 0 && map[x][y-1] != '#' && !v[x][y-1]) dfs(x,y-1);}int main(){ int i, j, x, y, k = 1; int t; cin>>t; while(t--) { memset(v,0,sizeof(v)); cin>>m>>n; for(i = 0;i < n;i++) { for(j = 0;j < m;j++) { cin>>map[i][j]; if(map[i][j] == '@') { x = i; y = j; } } } sum = 0; dfs(x,y); cout<<"Case "<<k<<": "<<sum<<endl; k++; } return 0;}
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- 两道面试题——进制各位数之和
- 【动态规划】最长上升子序列及其相关思想
- 1018. Public Bike Management (30)
- 滑雪 POJ
- HDU_【2017 Multi-University Training Contest 2】——1001 Is Derek lying?
- LightOJ
- spintf定义与作用
- 7.29学习总结
- js学习—在canvas画布上的粒子扩散
- 数学基础:HDU2802-F(N)(寻找循环节)
- HDU2222[Keywords Search] AC自动机裸题
- 华为机试——求int型正整数在内存中存储时1的个数
- 利用github和hexo搭建个人博客
- Bash read 接收键盘输入