coderforces 690D1 - The Wall (easy)(DFS)
2016-08-16 15:35
405 查看
D1. The Wall (easy)
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
"The zombies are lurking outside. Waiting. Moaning. And when they come..."
"When they come?"
"I hope the Wall is high enough."
Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for
help. Go forth and explore the wall! Report how many disconnected segments there are.
The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks
high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between,
is C columns wide.
Input
The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100.
The next R lines provide a description of the columns as follows:
each of the R lines contains a string of length C,
the c-th character of line r is B if
there is a brick in column c and row R - r + 1,
and . otherwise.
The input will contain at least one character B and it will be valid.
Output
The number of wall segments in the input configuration.
Examples
input
output
input
output
input
output
input
output
input
output
input
output
Note
In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.
// 简单DFS
#include<cstdio>
#include<cstring>
char map[110][110];
bool vis[110][110];
int n,m;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void dfs(int x,int y)
{
if(x < 0 || x >= n || y < 0 || y >= m || map[x][y] != 'B' || vis[x][y])
return;//
vis[x][y] = true;
for(int i = 0; i < 4 ; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
dfs(xx,yy);
}
}
int main()
{
int cnt = 0;
scanf("%d%d",&n,&m);
for(int i = 0 ; i < n ; i++)
scanf("%s",map[i]);
memset(vis,false,sizeof(vis));
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ; j++)
{
if(map[i][j] == 'B' && !vis[i][j])
{
dfs(i,j);
cnt++;
}
}
printf("%d\n",cnt);
return 0;
}
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
"The zombies are lurking outside. Waiting. Moaning. And when they come..."
"When they come?"
"I hope the Wall is high enough."
Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for
help. Go forth and explore the wall! Report how many disconnected segments there are.
The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks
high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between,
is C columns wide.
Input
The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100.
The next R lines provide a description of the columns as follows:
each of the R lines contains a string of length C,
the c-th character of line r is B if
there is a brick in column c and row R - r + 1,
and . otherwise.
The input will contain at least one character B and it will be valid.
Output
The number of wall segments in the input configuration.
Examples
input
3 7 ....... ....... .BB.B..
output
2
input
4 5 ..B.. ..B.. B.B.B BBB.B
output
2
input
4 6 ..B... B.B.BB BBB.BB BBBBBB
output
1
input
1 1B
output
1
input
10 7 ....... ....... ....... ....... ....... ....... ....... ....... ...B... B.BB.B.
output
3
input
8 8 ........ ........ ........ ........ .B...... .B.....B .B.....B .BB...BB
output
2
Note
In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.
// 简单DFS
#include<cstdio>
#include<cstring>
char map[110][110];
bool vis[110][110];
int n,m;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void dfs(int x,int y)
{
if(x < 0 || x >= n || y < 0 || y >= m || map[x][y] != 'B' || vis[x][y])
return;//
vis[x][y] = true;
for(int i = 0; i < 4 ; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
dfs(xx,yy);
}
}
int main()
{
int cnt = 0;
scanf("%d%d",&n,&m);
for(int i = 0 ; i < n ; i++)
scanf("%s",map[i]);
memset(vis,false,sizeof(vis));
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ; j++)
{
if(map[i][j] == 'B' && !vis[i][j])
{
dfs(i,j);
cnt++;
}
}
printf("%d\n",cnt);
return 0;
}
相关文章推荐
- codeforces 690D1 D1. The Wall (easy)(dfs)
- 【DFS】Gym - 101142C - CodeCoder vs TopForces
- CoderForces 510B (dfs)
- CodeForces 690D1 The Wall (easy) (判断连通块的数量)
- GYM 101142 C.CodeCoder vs TopForces(dfs)
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
- Codeforces Round #336 (Div. 2) 608D Zuma(dfs+dp)
- coderforces 348A Mafia(二分)
- Coderforces 617C Watering Flowers 【暴力】
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Coderforces 374 (Div2)
- (DFS)Educational Codeforces Round 28 E. Chemistry in Berland
- 文章标题 Coderforces 343D : Water Tree(dfs序+线段树)
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- 【CoderForces】#296 Div2 C(线段树+set)
- Coderforces Taxi
- Educational Codeforces Round 31 C. Bertown Subway dfs
- Codeforces Round #245 (Div. 2) A - Points and Segments (easy)
- AtCoder Grant Contest 10 F - Tree Game 博弈 dfs