leetcode~扫雷(Scala版)

leetcode~扫雷(Scala版)

算法原理:

  1. 先判断点击点
  2. 然后以点击点开始,作为当前点,判断周围情况,改变该点的值
  3. 然后以点击点的八个方位,执行深度优先搜索,比如以点击点的上方位点作为当前点,然后跳到步骤2继续,

代码:

import scala.collection.mutable.ArrayBuffer
import scala.math.abs
import scala.util.control.Breaks.{break, breakable}

object mineSweep {
    def main(args: Array[String]): Unit = {
        val board = Array(
            Array('E', 'E', 'E', 'E', 'E','E', 'E', 'E'),
            Array('E', 'E', 'E', 'E', 'E','E', 'E', 'M'),
            Array('E', 'E', 'M', 'E', 'E','E', 'E', 'E'),
            Array('M', 'E', 'E', 'E', 'E','E', 'E', 'E'),
            Array('E', 'E', 'E', 'E', 'E','E', 'E', 'E'),
            Array('E', 'E', 'E', 'E', 'E','E', 'E', 'E'),
            Array('E', 'E', 'E', 'E', 'E','E', 'E', 'E'),
            Array('E', 'E', 'M', 'M', 'E','E', 'E', 'E')
        )
        val click = Array(0,0)
        val checkboard = this.updateBoard(board,click)
        checkboard.foreach{
            row=> row.foreach(print)
                println()
        }
    }
    def updateBoard(board: Array[Array[Char]], click: Array[Int]): Array[Array[Char]] = {
        if (board(click(0))(click(1))== 'M'){
            board(click(0))(click(1)) = 'X'
            return board
        }
        else if(board(click(0))(click(1)) == 'B'){
            return board
        }
        this._updateBoard(board,(click(0),click(1)))
        board
    }
    def _updateBoard(board:Array[Array[Char]],click:(Int,Int)):Boolean={
        var (row,col) = click.copy()
        val m = 'M'
        val b = 'B'
        val e = 'E'
        val x = 'X'
        val (height,width) = (board.length,board(0).length)
        if (row < 0 || row > height - 1 || col < 0 || col > width-1) {
            return false
        }
        if (board(row)(col) == m){
            false
        }
        else if(board(row)(col) == b) {
            false
        }
        else if(board(row)(col).isDigit) {
            false
        }
        else {
            var arounds = _foundArounds(board,click)
            var counts = 0
            for((nextRow,nextCol) <- arounds) {
                if (abs(row - nextRow) <= 1 && abs(col - nextCol) <= 1) {
                    breakable {
                        if (board(nextRow)(nextCol) == m) counts += 1
                        else if (board(nextRow)(nextCol) == e) {
                        }
                        else if (board(nextRow)(nextCol) == b || board(nextRow)(nextCol).isDigit) {}
                    }
                }
            }
            if(counts==0) {
                board(row)(col) = b
//                println()
//                board.foreach{
//                    row=> row.foreach(print)
//                        println()
//                }
//                println()
                var hasPath = _updateBoard(board,(row-1,col)) ||
                    _updateBoard(board,(row-1,col+1)) ||
                    _updateBoard(board,(row,col+1)) ||
                    _updateBoard(board,(row+1,col+1)) ||
                    _updateBoard(board,(row+1,col)) ||
                    _updateBoard(board,(row+1,col-1)) ||
                    _updateBoard(board,(row,col-1)) ||
                    _updateBoard(board,(row-1,col-1))
                hasPath
            }
            else{
                board(row)(col) = counts.toString.charAt(0)
                false
            }
        }
    }
    def _foundArounds(board:Array[Array[Char]], click:(Int,Int),
                     ):ArrayBuffer[(Int,Int)]={
        val (height,width) = (board.length,board(0).length)
        val (currow,curcol) = click.copy()

        val arounds:ArrayBuffer[(Int,Int)] = new ArrayBuffer[(Int,Int)]()

        for(i <- -1 to 1) {
            var prepareRow = currow + i
            breakable { // REW:Continue 的 scala实现
                if (prepareRow < 0 || prepareRow > height - 1) {
                    break()
                }

                for (j <- -1 to 1) {
                    var prepareCol = curcol + j
                    breakable {
                        if (prepareCol < 0 || prepareCol > width - 1) {
                            break()
                        }
                        //等于e、m 加入arounds
                        breakable {
                            if (board(prepareRow)(prepareCol) == 'B' || board(prepareRow)(prepareCol).isDigit
                                || (prepareRow == currow && prepareCol == curcol)
                                || arounds.contains((prepareRow,prepareCol))
                            ){
                                break()
                            }
                            arounds.append((prepareRow, prepareCol))
                        }
                    }
                }
            }
        }
        arounds
    }
}

你可能感兴趣的:(刷题)