洛谷1596 [USACO10OCT]湖计数Lake Counting

题面

  • 这道题直接大暴搜即可,不管$bfs$还是$dfs$都可以,但是$dfs$好打一点,所以就用$dfs$了。

  • Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
using namespace std;
int cmp[8][2]={{0,1},{1,0},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
int n,m,ans;
char data[105][105];
bool book[105][105];
void dfs(int x,int y){
book[x][y]=true;
for(int i=0;i<8;i++){
int tx=x+cmp[i][0];
int ty=y+cmp[i][1];
if(data[tx][ty]=='W'&&!book[tx][ty])
dfs(tx,ty);
}
return;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin >>data[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(data[i][j]=='W'&&!book[i][j]){
ans++;
dfs(i,j);
}
printf("%d\n",ans);
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×