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:
- If a mine ('M') is revealed, then the game is over - change it to 'X'.
- 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.
- 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.
- 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; }};
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- Minesweeper
- 10189 - Minesweeper
- UVa10189 Minesweeper
- UVa10189 - Minesweeper
- 10189 - Minesweeper
- uva minesweeper
- 110102 MineSweeper
- MINESWEEPER题解
- 10189 - Minesweeper
- java中用poi导入excel文件到数据库总结
- 经典回溯-安装摄像头
- java web开发一个帐号同一时间只能一个人登录(单点登录)
- 【MATLAB】多个mat文件合成一个mat文件
- sqlcipher windows 平台编译方法
- Minesweeper
- 前端基础进阶(十一):详细图解jQuery对象,以及如何扩展jQuery插件
- 匿名类
- hdu 2021 发工资咯:)
- CentOS 7关闭默认防火墙启用iptables防火墙
- code forces 735D 数论(哥德巴赫猜想)
- 第二十二届华罗庚金杯少年数学邀请赛 决赛(初中二年级组)部分试题解答
- Pandas GroupBy 分组(分割-应用-组合)
- 元素的固定定位