[Leetcode] 419. Battleships in a Board 解题报告

题目

Given an 2D board, count how many 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?

思路

这道题目完全可以用union-find来解决,不过由于battleship的特殊形式,用union-find似乎有点杀鸡用牛刀。实际上我们可以用更简单的方法来实现:如果按照从左到右,从上到下的顺序遍历这个方格,那么对于每个battleship,我们首先一定会遇到它的舰首,每当遇到一个舰首时,我们一方面更新ret值,另一方面将该舰的其余部分全部置为‘.’,这样可以。避免后面的重复计算。这种方法算是one-pass,也没有使用额外的存储空间,但是在运行的过程中改变了board的值。

在Follow up中问我们是否能够不修改board的值?答案是可以的:我们每次碰到一个‘X’,首先判断该位置是不是舰首,如果是舰首,则ret值更新;否则说明该舰在前面已经被计算过了,直接跳过。Follow up的代码我就不上了,有兴趣的读者可以自己在下面代码的基础上实现。

代码

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        if (board.size() == 0 || board[0].size() == 0) {
            return 0;
        }
        int row_num = board.size(), col_num = board[0].size();
        int ret = 0;
        for (int y = 0; y < row_num; ++y) {
            for (int x = 0; x < col_num; ++x) {
                if (board[y][x] == 'X') {
                    ++ret;
                    removeBattleShip(board, x, y);
                }
            }
        }
        return ret;
    }
private:
    void removeBattleShip(vector<vector<char>> &board, int x, int y) {
        int row_num = board.size(), col_num = board[0].size();
        board[y][x] = '.';
        while (x < col_num - 1 && board[y][x + 1] == 'X') {
            board[y][++x] = '.';
        }
        while (y < row_num - 1 && board[y + 1][x] == 'X') {
            board[++y][x] = '.';
        }
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值