首页 > 代码库 > leetcode529
leetcode529
public class Solution { //DFS public char[,] UpdateBoard(char[,] board, int[] click) { int m = board.GetLength(0), n = board.GetLength(1); int row = click[0], col = click[1]; if (board[row, col] == ‘M‘) { // Mine board[row, col] = ‘X‘; } else { // Empty // Get number of mines first. int count = 0; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (i == 0 && j == 0) continue; int r = row + i, c = col + j; if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue; if (board[r, c] == ‘M‘ || board[r, c] == ‘X‘) count++; } } if (count > 0) { // If it is not a ‘B‘, stop further DFS. board[row, col] = (char)(count + ‘0‘); } else { // Continue DFS to adjacent cells. board[row, col] = ‘B‘; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (i == 0 && j == 0) continue; int r = row + i, c = col + j; if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue; if (board[r, c] == ‘E‘) UpdateBoard(board, new int[] { r, c }); } } } } return board; } ////BFS //public char[,] UpdateBoard(char[,] board, int[] click) //{ // int m = board.GetLength(0), n = board.GetLength(1); // Queue<int[]> queue = new Queue<int[]>(); // queue.Enqueue(click); // while (queue.Count > 0) // { // int[] cell = queue.Dequeue(); // int row = cell[0], col = cell[1]; // if (board[row, col] == ‘M‘) // { // Mine // board[row, col] = ‘X‘; // } // else // { // Empty // // Get number of mines first. // int count = 0; // for (int i = -1; i < 2; i++) // { // for (int j = -1; j < 2; j++) // { // if (i == 0 && j == 0) continue; // int r = row + i, c = col + j; // if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue; // if (board[r, c] == ‘M‘ || board[r, c] == ‘X‘) count++; // } // } // if (count > 0) // { // If it is not a ‘B‘, stop further DFS. // board[row, col] = (char)(count + ‘0‘); // } // else // { // Continue BFS to adjacent cells. // board[row, col] = ‘B‘; // for (int i = -1; i < 2; i++) // { // for (int j = -1; j < 2; j++) // { // if (i == 0 && j == 0) continue; // int r = row + i, c = col + j; // if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue; // if (board[r, c] == ‘E‘) // { // queue.Enqueue(new int[] { r, c }); // board[r, c] = ‘B‘; // Avoid to be added again. // } // } // } // } // } // } // return board; //} }
https://leetcode.com/problems/minesweeper/#/description
leetcode529
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。