zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    线程、进程、死锁
    堆和栈的区别
    用winrar解压时提示无法设置安全数据 拒绝访问的解决方法
    句子迷
    【Sublime Text3】sublime 推荐必备插件与常用快捷键说明
    【计算机科学】知乎上关于计算机科学、程序语言一些有用的回答~(不定期更新)
    【SQL 函数】日期函数、类型转换、字符串函数
    【Sql Server函数】日期时间函数、日期查询今天、昨天、7天内、30天的数据
    【JAVA环境配置免安装】如何由jdk的安装版本改成非安装版本
    【SQL 社区】一些有用的SQL社区!(持续更新~~)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15773483.html
Copyright © 2011-2022 走看看