[LeetCode]Battleships in a Board 战舰数目

声明:原题目转载自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
...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?

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; } 
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值