J - Guilty Prince
2015-08-14 20:04
387 查看
Description
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
#include <iostream>
#include <cstdio>
using namespace std;
#define MAx 500
int n,m,num=0;
char oil[25][25];
int oil1[4][2]=
{
0,1,
0,-1,
1,0,
-1,0
};
void dfs(int x, int y)
{
oil[x][y]='#';
for(int i = 0; i < 4; i++)
{
int nx = x + oil1[i][0];
int ny = y + oil1[i][1];
if(nx >= 0 && nx < n && ny >= 0&& ny < m && oil[nx][ny]=='.')
{
dfs(nx,ny);
num++;
}
}
}
int main()
{
int number,i,j,x,y;
scanf("%d",&number);
for(int a = 1;a <=number; a++)
{
int countt=0;
scanf("%d%d",&m,&n);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
cin>>oil[i][j];
if(oil[i][j]=='@')
{
x = i;
y = j;
}
}
dfs(x,y);
printf("Case %d: %d\n",a,num+1);
num=0;
}
return 0;
}
此题为数点,‘#’为墙,利用深度优先搜索,有四个方向 我们用数组控制好了之后 只需要把走过的地方换成‘#’即可,继续遍历到结束 。
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
#include <iostream>
#include <cstdio>
using namespace std;
#define MAx 500
int n,m,num=0;
char oil[25][25];
int oil1[4][2]=
{
0,1,
0,-1,
1,0,
-1,0
};
void dfs(int x, int y)
{
oil[x][y]='#';
for(int i = 0; i < 4; i++)
{
int nx = x + oil1[i][0];
int ny = y + oil1[i][1];
if(nx >= 0 && nx < n && ny >= 0&& ny < m && oil[nx][ny]=='.')
{
dfs(nx,ny);
num++;
}
}
}
int main()
{
int number,i,j,x,y;
scanf("%d",&number);
for(int a = 1;a <=number; a++)
{
int countt=0;
scanf("%d%d",&m,&n);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
cin>>oil[i][j];
if(oil[i][j]=='@')
{
x = i;
y = j;
}
}
dfs(x,y);
printf("Case %d: %d\n",a,num+1);
num=0;
}
return 0;
}
此题为数点,‘#’为墙,利用深度优先搜索,有四个方向 我们用数组控制好了之后 只需要把走过的地方换成‘#’即可,继续遍历到结束 。
相关文章推荐
- Codeforces 570D TREE REQUESTS dfs序+树状数组
- 7 Types of Regression Techniques you should know!
- Hadoop之SequenceFile
- hdu - 2667 Proving Equivalences(强连通)
- ListView中requestLayout执行流程解析
- OC - NSValueAndNSNumber
- UIView如何管理它的子视图
- IOS UIGraphicsBeginImageContext 使用简介
- iOS arc与非arc混编 以及设置UINavigationBar的高度
- UI-父视图和子视图之间的关系
- ios UISlider总结
- Django1.8:403错误:CSRF verification failed. Request aborted.
- 在AWS上为1.25亿用户实现移动应用个性化
- UINavigationController小知识点
- DNS服务器性能测试(基于queryperf)
- iOS UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- 【UVa 540】Team Queue
- iOS 归档UIImageView后,再解档,imageview的图片出现自动旋转
- Codeforces Round #250 D - The Child and Sequence/[TYVJ3838] DQS和序列(by 帝江&Darkfalmes)
- iOS基础-UIKit框架-多控制器管理-Segue