题目要求 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.
解题思路 注意这个题目里限定了甲板上存在的一定是符合规范的战舰,也就是不会存在相邻或者拐弯的情况。有了这个限定,解题就比较容易,遍历一下数组,我们只需要找到战舰起始点,就可以统计出来战舰的熟练。刚开始,我没有理解透这个限定,用了DFS的方法也能完成题目。
参考题解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 class Solution : def countBattleships (self, board ): """ :type board: List[List[str]] :rtype: int """ count = 0 rowStop = 0 columnStop = 0 x = 0 y = 0 rowSize = len (board) columnSize = len (board[0 ]) while (x < rowSize): while (y < columnSize): if ( board[x][y] == "X" ): if ( ( y == 0 or board[x][y-1 ] != "X" ) and ( x == 0 or board[x-1 ][y] != "X" ) ): count = count + 1 y = y + 1 y = 0 x = x + 1 return count def findEnd (self, board, x, y, direction = "" ): if ( x >= len (board) or y >= len (board[x])): return -1 RightEnd = None DownEnd = None if ( direction == "R" ): if (board[x][y] == "X" ): if (y+1 == len (board[x])): return -1 return self.findEnd(board,x,y+1 ,"R" ) elif (board[x][y] == "." ): return y elif ( direction == "D" ): if (board[x][y] == "X" ): if (x+1 ==len (board)): return -1 return self.findEnd(board,x+1 ,y,"D" ) elif (board[x][y] == "." ): return x else : if (board[x][y] == "X" ): RightEnd = self.findEnd(board,x,y+1 , "R" ) DownEnd = self.findEnd(board,x+1 ,y, "D" ) elif (board[x][y] == "." ): RightEnd = y DownEnd = x if (DownEnd != x and (x-1 >= 0 )): if (board[x-1 ][y] == "X" ): return False if (RightEnd != y and (y-1 >= 0 )): if (board[x][y-1 ] == "X" ): return False if ( RightEnd == y and DownEnd == x ): return False elif ( RightEnd >= y + 1 and DownEnd >= x + 1 ): return 1 elif ( RightEnd == -1 and DownEnd == -1 ): return 1 elif ( RightEnd == -1 and DownEnd >= x + 1 ): return 1 elif ( RightEnd >= y + 1 and DownEnd == -1 ): return 1 else : return False
参考资料: 1、图的基本算法(BFS和DFS) 2、邻接矩阵 3、Battleships in a board