zoukankan      html  css  js  c++  java
  • Week 5

    529.Minesweeper

    Let's play the minesweeper game (Wikipedia, online game)!
    You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.
    Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return >the board after revealing this position according to the following rules:
    1.If a mine ('M') is revealed, then the game is over - change it to 'X'.
    2.If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
    3.If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
    4.Return the board when no more squares will be revealed.

    Example 1:
    Input: 
    
    [['E', 'E', 'E', 'E', 'E'],
     ['E', 'E', 'M', 'E', 'E'],
     ['E', 'E', 'E', 'E', 'E'],
     ['E', 'E', 'E', 'E', 'E']]
    
    Click : [3,0]
    
    Output: 
    
    [['B', '1', 'E', '1', 'B'],
     ['B', '1', 'M', '1', 'B'],
     ['B', '1', '1', '1', 'B'],
     ['B', 'B', 'B', 'B', 'B']]
    
    Explanation:
    
    Example 2:
    Input: 
    
    [['B', '1', 'E', '1', 'B'],
     ['B', '1', 'M', '1', 'B'],
     ['B', '1', '1', '1', 'B'],
     ['B', 'B', 'B', 'B', 'B']]
    
    Click : [1,2]
    
    Output: 
    
    [['B', '1', 'E', '1', 'B'],
     ['B', '1', 'X', '1', 'B'],
     ['B', '1', '1', '1', 'B'],
     ['B', 'B', 'B', 'B', 'B']]
    

    Note:
    The range of the input matrix's height and width is [1,50].
    The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
    The input board won't be a stage when game is over (some mines have been revealed).
    For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

    这道题总体而言是一个DFS算法,从click on的位置开始向周围八个方向开始搜索。

    Solution:

    #include<string>
    #include<iostream>
    #include<vector>
    #include<list>
    using namespace std;
    class Solution {
    public:
      vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        if (board[click[0]][click[1]] == 'M') {
          board[click[0]][click[1]] = 'X';
        } else {
          reveal(board, click[0], click[1]);
        }
        return board;
      }
    
      bool ifInBoard(vector<vector<char>> board, int x, int y) {
        return (x >= 0 && y >= 0 && x < board.size() && y < board[0].size());
      }
      
      int searchAndCount(vector<vector<char>> board, int x, int y) {
        int count = 0;
        if (ifInBoard(board, x - 1, y - 1) && board[x - 1][y - 1] == 'M') count++;
        if (ifInBoard(board, x    , y - 1) && board[x    ][y - 1] == 'M') count++;
        if (ifInBoard(board, x + 1, y - 1) && board[x + 1][y - 1] == 'M') count++;
        if (ifInBoard(board, x - 1, y    ) && board[x - 1][y    ] == 'M') count++;
        if (ifInBoard(board, x + 1, y    ) && board[x + 1][y    ] == 'M') count++;
        if (ifInBoard(board, x - 1, y + 1) && board[x - 1][y + 1] == 'M') count++;
        if (ifInBoard(board, x    , y + 1) && board[x    ][y + 1] == 'M') count++;
        if (ifInBoard(board, x + 1, y + 1) && board[x + 1][y + 1] == 'M') count++;
        return count;
      }
      
      void reveal(vector<vector<char>>& board, int x, int y) {
        if (ifInBoard(board, x, y)) {
          if (board[x][y] == 'E') {
            int count = searchAndCount(board, x, y);
            if (count > 0) {
              board[x][y] = count + '0';
            } else {
              board[x][y] = 'B';
              reveal(board, x - 1, y - 1);
              reveal(board, x, y - 1);
              reveal(board, x + 1, y - 1);
              reveal(board, x - 1, y);
              reveal(board, x + 1, y);
              reveal(board, x - 1, y + 1);
              reveal(board, x, y + 1);
              reveal(board, x + 1, y + 1);
            }
          }
        }
      }
    
    };
    
    //test code
    int main() {
      Solution a;
      vector<char> row = { 'E', 'E', 'E', 'E', 'E' };
      vector<vector<char>> board;
      board.push_back(row);
      row[2] = 'M';
      board.push_back(row);
      row[2] = 'E';
      board.push_back(row); 
      board.push_back(row);
      vector<int> temp = { 3, 0 };
      board = a.updateBoard(board, temp);
      for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
          cout << board[i][j] << ' ';
        }
        cout << endl;
      }
      system("pause");
    }
  • 相关阅读:
    PDF上添加水印
    java调用POI读取Excel
    搭建Linux的VMware Workstation Pro
    js中两种定时器的设置及清除
    SUI使用经验
    List集合与Array数组之间的互相转换
    jquery操作select
    jquery操作CheckBox
    时间格式
    java 获取路径的各种方法
  • 原文地址:https://www.cnblogs.com/JerryChan31/p/7648431.html
Copyright © 2011-2022 走看看