声明:原题目转载自LeetCode,解答部分为原创
Problem :
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) orNx1
(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 ...XIn the above board there are 2 battleships.
Invalid Example:
...X XXXX ...XThis 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?
Solution:
思路:用BFS的方法遍历二维矩阵,本题的关键是准确判断图形符合要求的条件。忙碌中,代码略显凌乱,请大家见谅,日后将重新整理。
代码如下:
#include
#include
#include
using namespace std;
class Solution {
public:
int row,col;
int judge_num = 1;
int num = 0;
struct pair
{
int x,y;
pair(int a, int b)
{
x = a;
y = b;
}
};
stack
route;
int countBattleships(vector
>& board) { row = board.size(); col = board[0].size(); vector
> visit(row); for(int i = 0 ; i < row ; i ++) { for(int j = 0 ; j < col ; j ++) { visit[i].push_back(0); } } for(int i = 0 ; i < row ; i ++) { for(int j = 0 ; j < col ; j ++) { if(visit[i][j] == 0 && board[i][j] == 'X') { num ++; pair node(i,j); route.push(node); // cout << "visit (" << i << "," << j << ")" << endl; // cout << "num = " << num << endl; bfs(i,j, board, visit); } } } if(judge_num > 0) return num; else return -1; } void bfs(int r, int c, vector
> &board, vector
> &visit) { visit[r][c] = 1; // right if(r < row && r >= 0 && c + 1 >= 0 && c + 1 < col && board[r][c + 1] == 'X') { if( visit[r][c + 1] == 1 ) { route.pop(); if(route.top().x != r || route.top().y != c + 1) { // cout << route.top().x << "," << route.top().y << endl; // cout << "break right (" << r << "," << c + 1 << ")" << endl; // cout << "num = " << num << endl; judge_num = -1; return; } } else { pair node(r, c + 1); route.push(node); // cout << "visit (" << r << "," << c + 1 << ")" << endl; // cout << "num = " << num << endl; bfs(r, c + 1, board, visit); } } //down if(r + 1 < row && r + 1 >= 0 && c < col && c >= 0 && board[r + 1][c] == 'X') { if(visit[r + 1][c] == 1) { route.pop(); if(route.top().x != r + 1 || route.top().y != c ) { // cout << route.top().x << "," << route.top().y << endl; // cout << "break down (" << r + 1 << "," << c << ")" << endl; // cout << "num = " << num << endl; judge_num = -1; return; } } else { pair node(r + 1, c); route.push(node); // cout << "visit (" << r + 1 << "," << c << ")" << endl; // cout << "num = " << num << endl; bfs(r + 1, c, board, visit); } } //left if(r < row && r >= 0 && c - 1 >= 0 && c - 1 < col && board[r][c - 1] == 'X') { if(visit[r][c - 1] == 1) { route.pop(); if(route.top().x != r || route.top().y != c - 1) { // cout << route.top().x << "," << route.top().y << endl; // cout << "break left (" << r << "," << c + 1 << ")" << endl; // cout << "num = " << num << endl; judge_num = -1; return; } } else { pair node(r, c - 1); route.push(node); // cout << "visit (" << r << "," << c - 1 << ")" << endl; // cout << "num = " << num << endl; bfs(r, c - 1, board, visit); } } //up if(r - 1 < row && r - 1 >= 0 && c >= 0 && c < col && board[r - 1][c] == 'X') { route.pop(); if(visit[r - 1][c] == 1) { if(route.top().x != r - 1 || route.top().y != c ) { // cout << route.top().x << "," << route.top().y << endl; // cout << "break up (" << r - 1 << "," << c << ")" << endl; // cout << "num = " << num << endl; judge_num = -1; return; } } else { pair node(r - 1, c); route.push(node); // cout << "visit (" << r - 1 << "," << c << ")" << endl; // cout << "num = " << num << endl; bfs(r - 1, c, board, visit); } } } }; int main() { Solution text; vector
> matrix_0(3); for(int i = 0 ; i < 3 ; i ++) { for(int j = 0 ; j < 4 ; j ++) { matrix_0[i].push_back('.'); } } matrix_0[0][0] = 'X'; matrix_0[0][3] = 'X'; matrix_0[1][3] = 'X'; matrix_0[2][3] = 'X'; cout << "Warn :: -1 means that it is invalid" << endl << endl; cout << "matrix_1 ::" << endl; for(int i = 0 ; i < matrix_0.size() ; i ++) { for(int j = 0 ; j < matrix_0[0].size() ; j ++) { cout << matrix_0[i][j] << " "; } cout << endl; } cout << "count of battleship is " << text.countBattleships(matrix_0) << endl << endl; matrix_0[0][0] = '.'; matrix_0[1][0] = 'X'; matrix_0[1][1] = 'X'; matrix_0[1][2] = 'X'; matrix_0[1][3] = 'X'; matrix_0[2][3] = 'X'; // matrix_0[][] = 'X'; // matrix_0[][] = 'X'; cout << "matrix_2 ::" << endl; for(int i = 0 ; i < matrix_0.size() ; i ++) { for(int j = 0 ; j < matrix_0[0].size() ; j ++) { cout << matrix_0[i][j] << " "; } cout << endl; } cout << "count of battleship is " << text.countBattleships(matrix_0) << endl << endl; return 0; }