DFS连通块问题–[USACO10OCT]Lake Counting S and 求细胞数量
为了更深入更熟悉dfs,今天练习了两道题 [USACO10OCT]Lake Counting S. 数细胞数量.尽可能写详细,这样不仅我也看懂,阅读文章的小伙伴也能看懂啦!!
例题1-[USACO10OCT]Lake Counting S
题目描述
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.
由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个NxM(1<=N<=100;1<=M<=100)网格图表示。每个网格中有水(‘W’) 或是旱地(’.’)。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。
输入格式
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.
第1行:两个空格隔开的整数:N 和 M 第2行到第N+1行:每行M个字符,每个字符是’W’或’.’,它们表示网格图中的一排。字符之间没有空格。
输出格式
Line 1: The number of ponds in Farmer John’s field.
一行:水坑的数量
输入输出样例
输入
输出
3
/*为什么这道dfs题跟平时的dfs模型不太一样呢 ?
原因是这道题要求找所有的水坑, dfs一次只能遍历一个水坑,我们要去找所有的水坑,所以就得外循环遍历每一个点*/
#include<stdio.h>
int n,m;
char a[105][105];
int next[9][2]={
{
0,0},{
0,1},{
1,1},{
1,0},{
1,-1},{
0,-1},{
-1,-1},{
-1,0},{
-1,1}};
int book[105];
int ans;
void dfs(int x,int y)
{
a[x][y]='.'; //搜索过的标记住
int tx,ty;
for(int i=1;i<=8;i++){
//八个方向
tx=x+next[i][0]; //x方向
ty=y+next[i][1]; //y方向
if(a[tx][ty]=='W'&&tx>0&&tx<=n&&ty>0&&ty<=m){
//防止越界
a[tx][ty]='.'; //搜索过的水标记成旱地,其实这里与上述的a[x][y]='.'重复了,但问题不大
dfs(tx,ty); //继续遍历
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j]; //字符用cin输入,字符用cin输入 ,字符用cin输入 ,重要的事情说三遍
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='W'){
//如果是w 则变为.
// a[i][j]='.';
ans++; //答案+1
dfs(i,j);//遍历一个点,由于是for循环,这样就能遍历完所有的点啦
}
}
}
printf("%d",ans);
return 0;
}
例题2-求细胞数量
这里为什么要多出一道求细胞数量题目呢?这是因为把同一类型的题目放在一起做,以后再见到这样类型的题目就可以做到 看到题目脑海里马上浮现出题解代码了
题目描述
一矩形阵列由数字 0 到 9 组成,数字 1 到 9 代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。
输入格式
第一行两个整数代表矩阵大小 n 和 m。
接下来 n 行,每行一个长度为 m 的只含字符 0 到 9 的字符串,代表这个 n×m 的矩阵。
输出格式
一行一个整数代表细胞个数。
输入输出样例
输入
4 10
0234500067
1034560500
2045600671
0000000089
输出
4
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int ans;
int n,m;
int next[5][2]={
{
0,0},{
0,1},{
1,0},{
0,-1},{
-1,0}}; //四个方向嘛
void dfs(int x,int y)
{
a[x][y]='0'; //把搜索过的细胞变为无,即0
int tx,ty;
for(int i=1;i<=4;i++){
//四个方向
tx=x+next[i][0];
ty=y+next[i][1];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]!='0'){
//防止越界
dfs(tx,ty); //继续遍历
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j]; //字符输入用cin 字符输入用cin 字符输入用cin
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!='0'){
//如果是细胞
ans++; //答案+1
dfs(i,j); 遍历一个点,由于是for循环,这样就能遍历完所有的点啦
}
}
}
printf("%d",ans);
return 0;
}
这道题相对简单,其实都是一样的套路,做了上面的[USACO10OCT]Lake Counting S,这道题就能闭着眼睛写出来了
加油,奥利给