Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (’.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
Input
Line 1: Two space-separated integers: N and M
Lines 2…N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.
Output
Line 1: The number of ponds in Farmer John’s field.
Sample Input
10 12
W…WW.
.WWW…WWW
…WW…WW.
…WW.
…W…
…W…W…
.W.W…WW.
W.W.W…W.
.W.W…W.
…W…W.
Sample Output
3
问有多少个水池
求几个连通块,遇到一个W 就将整个水池dfs一遍,记录dfs的次数
#include
#include
using namespace std;
int n,m;
char ch[105][105];
void dfs(int r,int c)
{
if(r>=n||r<0|| c>=m||c<0) return ; //3个终止条件
else if(ch[r][c]!='W') return ;
else ch[r][c]='.';
for(int dr=-1;dr<=1;dr++){
for(int dc=-1;dc<=1;dc++){
if(dr||dc) { //不同时为 0
dfs(r+dr,c+dc);
}
}
}
return ;
}
int main()
{
//int n,m;
cin>>n>>m;
int cnt=0;
for(int i=0;i<n;i++) scanf("%s",ch[i]); //以字符串形式输入每一行
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(ch[i][j]=='W'){ dfs(i,j); cnt++; } //大写 W
}
}
cout<<cnt<<endl;
return 0;
}
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#
…
…
…
…
…
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.
…
11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13
问能到达的黑砖数
找到人站的位置后,分别向上下左右dfs,同时标记走过的黑砖,每走到一块黑砖,数量+1
#include
#include
using namespace std;
int R,C;
int cnt=0; //
char ch[25][25];
int dr[4]={-1,1,0,0}; //方向数组
int dc[4]={0,0,-1,1};
void dfs(int r,int c)
{
if(r<0||c<0||r>=R||c>=C) return ;
if(ch[r][c]=='#'||ch[r][c]=='Y') return;//若用!='.' 刚开始是 @ 直接被 return 出来。。
ch[r][c]='Y'; // Y标记已走过
cnt++;
for(int i=0;i<4;i++){
dfs(r+dr[i],c+dc[i]);
}
return ;
}
int main()
{
while(cin>>C>>R , C||R){
cnt=0; //清空
for(int i=0;i<R;i++) scanf("%s",ch[i]);
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
if(ch[i][j]=='@') { dfs(i,j); break;
}
}
}
cout<<cnt<<endl;
}
return 0;
}