#include <bits/stdc++.h>
using namespace std;
int r, c, ans;
string a[1001];
int f[1001][1001];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int x, int y) {
f[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nr = x + dx[i];
int nc = y + dy[i];
if (nr < 0 || nr > r || nc < 0 || nc > c || f[nr][nc] == 1) {
continue;
}
dfs(nr, nc);
f[nr][nc] = 1;
}
}
int main() {
cin.tie(0);
cin >> r >> c;
for (int i = 0; i < r; i++) {
cin >> a[i];
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (a[i][j] == '#') {
f[i][j] = 0;
} else {
f[i][j] = 1;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (f[i][j] == 0) {
ans++;
dfs(i, j);
}
}
}
if (ans > 0)
cout << "There are " << ans << " ships.";
else
cout << "Bad placement.";
return 0;
}
然后就R162574079 记录详情了
dalao帮一下吧