-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleship in a board.cpp
31 lines (31 loc) · 1020 Bytes
/
Battleship in a board.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
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
if(board.empty()) return {};
int row = board.size(), col = board[0].size();
int res = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(board[i][j] == 'X'){
int x = i - 1, y = j;
bool top = false, left = false;
if(x < 0 || y < 0 || x >= row || y >= col){
top = true;
}
else if(board[x][y] == '.'){
top = true;
}
x = i, y = j - 1;
if(x < 0 || y < 0 || x >= row || y >= col){
left = true;
}
else if(board[x][y] == '.'){
left = true;
}
if(top && left) res++;
}
}
}
return res;
}
};