[Luogu] P1331 海战
Luogu P1331 海战
这个题大概是找图形规律。首先,因为如果"Bad placement"的话,四个格里一定会有三个是'#'。其次,如果有船的话,这一块矩形的右下角一格的右边、下边一格一定都是空的,要不然就"Bad placement"了。
#include <algorithm>
#include <iostream>
using namespace std;
int r, c, tot;
int map[1010][1010];
char tmp;
inline bool calc(int x, int y) {
return ((map[x][y] + map[x + 1][y] + map[x][y + 1] + map[x + 1][y + 1]) == 3);
}
int main() {
ios::sync_with_stdio(false);
cin >> r >> c;
for (int i = 1; i <= r; ++i)
for (int j = 1; j <= c; ++j) {
cin >> tmp;
map[i][j] = tmp == '#' ? 1 : 0;
}
for (int i = 1; i <= r; ++i)
for (int j = 1; j <= c; ++j) {
if(calc(i, j)) {cout << "Bad placement.\n"; return 0;}
if(map[i][j] && !map[i + 1][j] && !map[i][j + 1]) tot++;
}
cout << "There are " << tot << " ships.\n";
return 0;
}