-
Notifications
You must be signed in to change notification settings - Fork 1
/
419.cpp
38 lines (35 loc) · 1.1 KB
/
419.cpp
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
class Solution {
public:
void search(vector<vector<char>> &board, vector<vector<int>> &color, int x, int y, int cnt) {
if (x < board.size() && y < board[x].size() && board[x][y] == 'X' && color[x][y] == 0) {
color[x][y] = cnt;
}
else {
return;
}
search(board, color, x + 1, y, cnt);
search(board, color, x, y + 1, cnt);
}
int countBattleships(vector<vector<char>>& board) {
int row = board.size();
vector<vector<int>> color(row);
for (int i = 0; i < row; i++) {
color[i].resize(board[i].size());
for (int j = 0; j < color[i].size(); j++) {
color[i][j] = 0;
}
}
int cnt = 1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < board[i].size(); j++) {
if (color[i][j] > 0)
continue;
if (board[i][j] == 'X') {
search(board, color, i, j, cnt);
cnt++;
}
}
}
return cnt - 1;
}
};