-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBattleshipInABoard_419.java
78 lines (69 loc) · 2.49 KB
/
BattleshipInABoard_419.java
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
package com.leetcode.problems.medium;
/**
* @author neeraj on 08/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class BattleshipInABoard_419 {
public static void main(String[] args) {
char[][] board = new char[][]{
{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}
};
// System.out.println(countBattleships(board));
System.out.println(countBattleshipsOptimized(board));
}
public static int countBattleshipsOptimized(char[][] board) {
int TOTAL_BATTLESHIPS = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
// We are on battleship
if(board[i][j] == '.') {
continue;
}
if( i > 0 && board[i-1][j] == 'X') {
continue;
}
if( j > 0 && board[i][j-1] == 'X') {
continue;
}
TOTAL_BATTLESHIPS++;
}
}
return TOTAL_BATTLESHIPS;
}
private static boolean isStartOfShip(char[][] board, int i, int j) {
if (i == 0) { // When in 1st Row, we can only check by looking into left that we are start of ship or not
return j == 0 || board[i][j - 1] != 'X' ? true : false;
}
if (j == 0) { // When in 1st Column but not in 1st Row, so we have to check just the top
return board[i - 1][j] != 'X' ? true : false;
}
return board[i][j - 1] != 'X' && board[i - 1][j] != 'X' ? true : false;
}
public static int countBattleships(char[][] board) {
int count = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 'X') {
dfs(board, i, j);
count++;
}
}
}
return count;
}
public static void dfs(char[][] board, int i, int j) {
board[i][j] = 'V';
if (isSafe(board, i - 1, j))
dfs(board, i - 1, j);
if (isSafe(board, i, j + 1))
dfs(board, i, j + 1);
if (isSafe(board, i + 1, j))
dfs(board, i + 1, j);
if (isSafe(board, i, j - 1))
dfs(board, i, j - 1);
}
public static boolean isSafe(char[][] board, int i, int j) {
return i >= 0 && j >= 0 && i < board.length && j < board[0].length && board[i][j] == 'X';
}
}