Battleships in a Board

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:

...X
XXXX
...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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值