-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1856.cc
91 lines (79 loc) · 2.54 KB
/
1856.cc
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//Name: Sea Battle
//Level: 2
//Category: シミュレーション
//Note:
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
bool isHash(const vector<string> &field, int r, int c) {
const int R = field.size();
const int C = field[0].size();
if(r < 0 || R <= r || c < 0 || C <= c) return false;
return field[r][c] == '#';
}
bool isShip(const vector<string> &field, int r, int c) {
const int R = field.size();
const int C = field[0].size();
if(r < 0 || R <= r || c < 0 || C <= c) return false;
return field[r][c] != '.';
}
int main() {
int R, C;
char buf[1002];
while(true) {
scanf(" %d %d ", &R, &C);
if(!R && !C) break;
vector<string> field(R);
for(int i = 0; i < R; ++i) {
fgets(buf, 1002, stdin);
buf[C] = '\0';
field[i] = string(buf);
}
int ans = 0;
bool bad = false;
for(int r = 0; r < R; ++r) {
for(int c = 0; c < C; ++c) {
if(field[r][c] == '#') {
//Search mode
//Determine width
int w = 0;
while(isHash(field, r, c+w)) {
field[r][c+w] = '.';
++w;
}
//Check for rows
int h = 1;
while(isHash(field, r+h, c)) {
//Check for rectangleness
for(int offc = 0; offc < w; ++offc) {
if(!isHash(field, r+h, c+offc)) {
bad = true;
goto end;
}
field[r+h][c+offc] = '$';
}
if(isShip(field, r+h, c-1) || isShip(field, r+h, c+w)) {
bad = true;
goto end;
}
++h;
}
//Check if next row is empty
for(int offc = -1; offc <= w; ++offc) {
if(isShip(field, r+h, c+offc)) {
bad = true;
goto end;
}
}
++ans;
}
}
}
end:
if(bad) cout << "Bad placement." << endl;
else cout << "There are " << ans << " ships." << endl;
}
return 0;
}