Battleships in a Board

来源:互联网 发布:阿里云免费套餐9.9 编辑:程序博客网 时间:2024/06/16 11:46

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:

  • You receive a valid board, made of only battleships or empty slots.
  • Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
  • At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.

Example:

X..X...X...X
In the above board there are 2 battleships.

Invalid Example:

...XXXXX...X
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.

Follow up:
Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?

思路1:这题表明了,战舰只能横着或者竖着摆,那么我们只需要扫描矩阵,找到战舰的头,头有什么特征就是:头的左边和上方不能是'X',那么就是头。

代码需要用排除法来写,为. 或者左边,上方为X的时候,不计算,其他时间都是头。

public class Solution {    public int countBattleships(char[][] board) {        if(board == null || board.length == 0) return 0;        int count = 0;        for(int i=0; i<board.length; i++){            for(int j=0; j<board[0].length; j++){                if(board[i][j] == '.' || (i>0 && board[i-1][j] == 'X') || (j>0 && board[i][j-1] == 'X')){                    continue;                }                count++;            }        }        return count;    }}

思路2:这题应该想到的解法就是number of island的变种解法;关键点在于如何判断战舰是横着,或者竖着的,那么方法就是把所有connnect 的点的横坐标和纵坐标 全部或起来,那么如果等于原来开始的点,那么就是一条战舰。这个思路很巧妙。注意参数的传递用array,这样dfs回来不会丢失;

public class Solution {    public int countBattleships(char[][] board) {        if(board == null || board.length == 0) return 0;        int count = 0;        boolean[][] visited = new boolean[board.length][board[0].length];        for(int i=0; i<board.length; i++){            for(int j=0; j<board[0].length; j++){                if(board[i][j] == 'X' && !visited[i][j]){                    int[] vertical = {0};                    int[] horizontal = {0};                    dfs(board, i, j, vertical, horizontal, visited);                    if(vertical[0] == i || horizontal[0] == j){                        count++;                    }                }            }        }        return count;    }        public void dfs(char[][] board, int i, int j, int[] vertical, int[] horizontal, boolean[][] visited){        if(i<0 || i>=board.length || j<0 || j>=board[0].length || visited[i][j] || board[i][j] == '.') return;        if(board[i][j] == 'X'){            visited[i][j] = true;            vertical[0] |= i; horizontal[0] |=j;            dfs(board, i+1, j, vertical, horizontal, visited);            dfs(board, i-1, j, vertical, horizontal, visited);            dfs(board, i, j-1, vertical, horizontal, visited);            dfs(board, i, j+1, vertical, horizontal, visited);        }    }}


0 0
原创粉丝点击