Minesweeper

来源:互联网 发布:php curl抓取网页数据 编辑:程序博客网 时间:2024/04/26 01:21

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']]Explanation:

代码:class Solution {public:    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {        int m=board.size();        int n=board[0].size();        if (m<0||n<0||click[0]>=m||click[1]>=n)            return board;        //If a mine ('M') is revealed, then the game is over - change it to 'X'.        int i=click[0], j=click[1];        if (board[i][j]=='M')        {            board[i][j]='X';            return board;        }                vector<vector<bool>> visited(m,vector<bool>(n,false));        scan(board,visited,i,j,m,n);        return board;    }        void scan(vector<vector<char>>& board,vector<vector<bool>>& visited,int i,int j,int m,int n)    {        if (i<0||j<0||i>=m||j>=n||visited[i][j]==true)            return;        visited[i][j]=true;        if (board[i][j]=='E')        {            //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.            int count=numofmine(board,i,j,m,n);            if (count!=0)            {                board[i][j]=count+'0';                return;            }            else            {                //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.                board[i][j]='B';                scan(board,visited,i+1,j,m,n);                scan(board,visited,i-1,j,m,n);                scan(board,visited,i,j+1,m,n);                scan(board,visited,i,j-1,m,n);                scan(board,visited,i+1,j+1,m,n);                scan(board,visited,i-1,j-1,m,n);                scan(board,visited,i-1,j+1,m,n);                scan(board,visited,i+1,j-1,m,n);            }        }            }    int numofmine(vector<vector<char>>& board, int i, int j, int m, int n)    {        int count=0;        if ((i-1)>=0&&board[i-1][j]=='M')            count++;        if ((i+1)<m&&board[i+1][j]=='M')            count++;        if ((j-1)>=0&&board[i][j-1]=='M')             count++;        if ((j+1)<n&&board[i][j+1]=='M')            count++;        if ((i-1)>=0&&(j-1)>=0&&board[i-1][j-1]=='M')            count++;        if ((i-1)>=0&&(j+1)<n&&board[i-1][j+1]=='M')            count++;        if ((i+1)<m&&(j-1)>=0&&board[i+1][j-1]=='M')            count++;        if ((i+1)<m&&(j+1)<n&&board[i+1][j+1]=='M')            count++;        return count;    }};

0 0
原创粉丝点击