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?

首先想到的是DFS,像island那题,找到一个X就dfs把X改为.,防止重复遍历,复习了一下写法,代码如下:

class Solution {
public:
    int x[4] = {0, 0, 1, -1};
    int y[4] = {1, -1, 0, 0};
    int countBattleships(vector<vector<char>>& board) {
        int n = board.size();
        int m = board[0].size();
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'X')  {
                    dfs(i, j, board);
                    count++;
                }
            }
        }
        return count;
    }
    void dfs(int i, int j, vector<vector<char>>& board) {
        board[i][j] = '.';
        for (int k = 0; k < 4; k++) {
            int dx = i + x[k];
            int dy = j + y[k];
            if (dx >=0 && dx < board.size() && dy >= 0 && dy < board[0].size() && board[dx][dy] == 'X') dfs(dx, dy, board);
        }
    }
};
但是follow up要求只能用O(1)额外空间和遍历一次数组,以及不能改变board内的值,所以要考虑,因为这道题的题目里说只可能存在横着或者竖着的一排,不会有交叉,所以我们可以从上向下一排一排遍历,然后只计算第一个出现的X,这个时候就要分类讨论什么时候是第一个出现的,什么时候是和别的X连着的:

i= 0, j = 0时若为X肯定是第一个,因为是左上角第一个格子,count++

然后第一行向右遍历,i=0,j!=0时,只要左边一格为X就是连着的,左边一格为.就是第一个,count++

然后到第二行,考虑第一列的情况,i!=0, j=0时,只要上面一格为X就是连着的,上面一格为.就是第一个,count++

然后考虑i!=0,j!=0的情况,当左边一格和上面一格有一个为X就是连着的,都为.就是第一个,count++

代码如下:

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int n = board.size();
        int m = board[0].size();
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'X') {
                    if (i == 0 && j == 0) count++;
                    else if (i == 0 && j != 0) {
                        if (board[i][j-1] == '.') count++;
                    }
                    else if (i != 0 && j==0) {
                        if (board[i-1][j] == '.') count++;
                    }
                    else {
                        if (board[i-1][j] == '.' && board[i][j-1] == '.') count++;
                    }
                }
            }
        }
        return count;
    }
};






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值