Battleships in a Board

给一个board, 里面的’X’代表船的位置, 船和船之间隔着至少一个’.’, 问有几艘船

这题就dfs一下即可. 搜过的mark成’Y’

class Solution {
    public int countBattleships(char[][] board) {
        int n = board.length;
        int m = board[0].length;
        int res = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(board[i][j] == 'X'){
                    res++;
                    dfs(board,i, j);
                }
            }
        }
        return res;
    }
    
    public void dfs(char[][] b, int i, int j){
        int[][] dirs = new int[][]{
            {0,1},{1,0},{0,-1},{-1,0}  
        };
        if(0 <= i && i < b.length && 0 <= j && j < b[0].length && b[i][j] == 'X'){
            b[i][j] = 'Y';
            for(int[] d : dirs) {
                dfs(b, i + d[0], j + d[1]);
            }
        }
    }
}