0419.cpp (467B)
1 class Solution { 2 public: 3 int countBattleships(const vector<vector<char>> &board) { 4 int res = 0; 5 for (int i = 0; i < board.size(); i++) { 6 for (int j = 0; j < board[0].size(); j++) { 7 if (board[i][j] == '.') continue; 8 if (i > 0 && board[i - 1][j] == 'X') continue; 9 if (j > 0 && board[i][j - 1] == 'X') continue; 10 res++; 11 } 12 } 13 return res; 14 } 15 };