1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| func updateBoard(board [][]byte, click []int) [][]byte { var dirX = []int{0, 1, 0, -1, 1, 1, -1, -1} var dirY = []int{1, 0, -1, 0, 1, -1, 1, -1}
if board[click[0]][click[1]] == 'M' { board[click[0]][click[1]] = 'X' return board } var dfs func([][]byte, int, int) dfs = func(board [][]byte, x, y int) { mCount := 0 for i := 0; i < 8; i++ { tx := x + dirX[i] ty := y + dirY[i] if tx < 0 || ty < 0 || tx >= len(board) || ty >= len(board[0]) { continue } if board[tx][ty] == 'M' { mCount++ } } if mCount > 0 { board[x][y] = byte(mCount + '0') } else { board[x][y] = 'B' for i := 0; i < 8; i++ { tx := x + dirX[i] ty := y + dirY[i] if tx < 0 || ty < 0 || tx >= len(board) || ty >= len(board[0]) || board[tx][ty] != 'E' { continue } dfs(board, tx, ty) } } } dfs(board, click[0], click[1]) return board }
func updateBoard(board [][]byte, click []int) [][]byte { var dirX = []int{0, 1, 0, -1, 1, 1, -1, -1} var dirY = []int{1, 0, -1, 0, 1, -1, 1, -1}
if board[click[0]][click[1]] == 'M' { board[click[0]][click[1]] = 'X' return board } var bfs func([][]byte, int, int) bfs = func(board [][]byte, x, y int) { q := [][]int{} vis := make([][]bool, len(board)) for i := 0; i < len(vis); i++ { vis[i] = make([]bool, len(board[0])) } q = append(q, []int{x, y}) vis[x][y] = true for i := 0; i < len(q); i++ { mCount, cx, cy := 0, q[i][0], q[i][1] for i := 0; i < 8; i++ { tx, ty := cx + dirX[i], cy + dirY[i] if tx < 0 || tx >= len(board) || ty < 0 || ty >= len(board[0]) { continue } if board[tx][ty] == 'M' { mCount++ } } if mCount > 0 { board[cx][cy] = byte(mCount + '0') } else { board[cx][cy] = 'B' for i := 0; i < 8; i++ { tx, ty := cx + dirX[i], cy + dirY[i] if tx < 0 || tx >= len(board) || ty < 0 || ty >= len(board[0]) || board[tx][ty] != 'E' || vis[tx][ty] { continue } q = append(q, []int{tx, ty}) vis[tx][ty] = true } } } } bfs(board, click[0], click[1])
return board }
|