-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleship.java
More file actions
35 lines (31 loc) · 1.23 KB
/
Battleship.java
File metadata and controls
35 lines (31 loc) · 1.23 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
public class Battleship {
public int countBattleships(char[][] board) {
// get grid dimensions and
// enrusre there is a grid
int m = board.length;
if (m == 0)
return 0;
int n = board[0].length;
int count = 0; // count the number of ships
// traverse the grid from top left to bottom right
// row by row
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// when encoutner an X
if (board[i][j] == 'X') {
// check to see if it part of a previously known ship
// by examining left and above this X
if ((i == 0 || board[i - 1][j] == '.') && (j == 0 || board[i][j - 1] == '.')) {
// if not part of another ship increment the counter
count++;
} // end if
} // end if
} // end for
} // end for
return count;
}
public static void main(String[] args) {
char[][] board = new char[][] { { 'X', '.', '.', 'X' }, { '.', '.', '.', 'X' }, { '.', '.', '.', 'X' } };
System.out.println(new Battleship().countBattleships(board));
}
}