- C++
洛谷P1596
- 2024-12-29 17:37:43 @
#include <iostream>
#include <vector>
using namespace std;
vector <string> lake;
int ans = 0;
int x[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, y[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(int dx, int dy) {
if(dx < 0 || dx >= lake.size() || dy < 0 || dy >= lake[0].size() || lake[dx][dy] == '.') {
return;
}
lake[dx][dy] = '.';
for(int i = 0; i < 8; ++i) {
dfs(dx + x[i], dy + y[i]);
}
return;
}
int main() {
int row,col;
scanf("%d%d",&row,&col);
for(int i = 0; i < row; ++i) {
string n;
cin >> n;
lake.push_back(n);
}
for(int i = 0; i < row; ++i) {
for(int t = 0; t < col; ++t) {
if(lake[i][t] == '.') {
continue;
}
++ans;
dfs(i,t);
}
}
printf("%d",ans);
return 0;
}
再不教新算法我真红温了
0 comments
No comments so far...