-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path499.cpp
More file actions
49 lines (45 loc) · 1.56 KB
/
499.cpp
File metadata and controls
49 lines (45 loc) · 1.56 KB
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
// Problem : 419. Battleships in a Board
// Link : https://leetcode.com/problems/battleships-in-a-board/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int shipCount = 0;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (board[i][j] == '.')
continue;
if (j + 1 < board[i].size() && board[i][j + 1] == 'X') {
board[i][j++] = '.';
while (j < board[i].size() && board[i][j] == 'X')
board[i][j++] = '.';
shipCount++;
} else if (i + 1 < board.size() && board[i + 1][j] == 'X') {
int iCopy = i;
board[iCopy++][j] = '.';
while (iCopy < board.size() && board[iCopy][j] == 'X')
board[iCopy++][j] = '.';
shipCount++;
} else if (j + 1 == board[i].size())
shipCount++;
else if (i + 1 == board.size())
shipCount++;
else {
shipCount++;
board[i][j] = '.';
}
}
}
cout << shipCount;
return shipCount;
}
};
int main() {
Solution ob;
vector<vector<char>> nums = {{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}};
ob.countBattleships(nums);
return 0;
}