529. Minesweeper
作者:互联网
use std::collections::LinkedList; /** 529. Minesweeper https://leetcode.com/problems/minesweeper/ Let's play the minesweeper game (Wikipedia, online game)! You are given an m x n char matrix board representing the game board where: . 'M' represents an unrevealed mine, . 'E' represents an unrevealed empty square, . 'B' represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals), . digit ('1' to '8') represents how many mines are adjacent to this revealed square, and . 'X' represents a revealed mine. You are also given an integer array click where click = [clickr, clickc] represents the next click position 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. You should change it to 'X'. 2. If an empty square 'E' with no adjacent mines is revealed, then change it to a 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: board = [["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"]] Constraints: 1. m == board.length 2. n == board[i].length 3. 1 <= m, n <= 50 4. board[i][j] is either 'M', 'E', 'B', or a digit from '1' to '8'. 5. click.length == 2 6. 0 <= clickr < m 7. 0 <= clickc < n 8. board[clickr][clickc] is either 'M' or 'E'. */ pub struct Solution {} impl Solution { /* Solution: BFS, scan 8 directions for every node, Time:O(m*n), Space:O(m*n); */ pub fn update_board(mut board: Vec<Vec<char>>, click: Vec<i32>) -> Vec<Vec<char>> { let (m, n) = (board.len() as i32, board[0].len() as i32); //dir is 8 rows and 2 columns let mut dir: [[i32; 2]; 8] = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, 1], [1, -1], [-1, 1]]; let mut queue: LinkedList<(usize, usize)> = LinkedList::new(); //check the x,y of click if a Mine first queue.push_back((click[0] as usize, click[1] as usize)); while !queue.is_empty() { let mut cur = queue.pop_front().unwrap(); if board[cur.0 as usize][cur.1 as usize] == 'M' { board[cur.0 as usize][cur.1 as usize] = 'X' } else { //check 8 direction of current position and count the number of Mine let mut count = 0; for d in &dir { let x = cur.0 as i32 + d[0]; let y = cur.1 as i32 + d[1]; if x < 0 || y < 0 || x >= m || y >= n { //check border case continue; } if board[x as usize][y as usize] == 'M' { count += 1; } } let cur_x = cur.0; let cur_y = cur.1; if count > 0 { //if current's neighbour is Mine, update current to count of Mine board[cur_x][cur_y] = ('0' as u8 + count) as char; } else { board[cur_x][cur_y] = 'B'; for d in &dir { let x = cur.0 as i32 + d[0]; let y = cur.1 as i32 + d[1]; if x < 0 || y < 0 || x >= m || y >= n { //check border case continue; } if board[x as usize][y as usize] == 'E' { board[x as usize][y as usize] = 'B'; //put new position to queue for next level queue.push_back((x as usize, y as usize)); } } } } } board } }
标签:cur,Minesweeper,click,let,board,usize,revealed,529 来源: https://www.cnblogs.com/johnnyzhao/p/15773483.html