voiddfs(vector<vector<char>>& board, int x, int y){ int cnt = 0; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; if (tx < 0 || tx >= board.size() || ty < 0 || ty >= board[0].size()) { continue; } // 不用判断 M,因为如果有 M 的话游戏已经结束了 cnt += board[tx][ty] == 'M'; } if (cnt > 0) { // 规则 3 board[x][y] = cnt + '0'; } else { // 规则 2 board[x][y] = 'B'; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; // 这里不需要在存在 B 的时候继续扩展,因为 B 之前被点击的时候已经被扩展过了 if (tx < 0 || tx >= board.size() || ty < 0 || ty >= board[0].size() || board[tx][ty] != 'E') { continue; } dfs(board, tx, ty); } } }
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) { int x = click[0], y = click[1]; if (board[x][y] == 'M') { // 规则 1 board[x][y] = 'X'; } else { dfs(board, x, y); } return board; } };
voiddfs(char** board, int x, int y) { int cnt = 0; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; if (tx < 0 || tx >= n || ty < 0 || ty >= m) { continue; } // 不用判断 M,因为如果有 M 的话游戏已经结束了 cnt += board[tx][ty] == 'M'; } if (cnt > 0) { // 规则 3 board[x][y] = cnt + '0'; } else { // 规则 2 board[x][y] = 'B'; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; // 这里不需要在存在 B 的时候继续扩展,因为 B 之前被点击的时候已经被扩展过了 if (tx < 0 || tx >= n || ty < 0 || ty >= m || board[tx][ty] != 'E') { continue; } dfs(board, tx, ty); } } }
char** updateBoard(char** board, int boardSize, int* boardColSize, int* click, int clickSize, int* returnSize, int** returnColumnSizes) { n = boardSize, m = boardColSize[0]; int x = click[0], y = click[1]; if (board[x][y] == 'M') { // 规则 1 board[x][y] = 'X'; } else { dfs(board, x, y); } *returnSize = n; **returnColumnSizes = malloc(sizeof(int*) * n); for (int i = 0; i < n; i++) { (*returnColumnSizes)[i] = m; } return board; }
voidbfs(char** board, int sx, int sy) { bool vis[n][m]; memset(vis, 0, sizeof(vis)); pair Q[n * m]; int l = 0, r = 1; Q[0].x = sx, Q[0].y = sy; vis[sx][sy] = true; while (l < r) { pair pos = Q[l++]; int cnt = 0, x = pos.x, y = pos.y; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; if (tx < 0 || tx >= n || ty < 0 || ty >= m) { continue; } // 不用判断 M,因为如果有 M 的话游戏已经结束了 cnt += board[tx][ty] == 'M'; } if (cnt > 0) { // 规则 3 board[x][y] = cnt + '0'; } else { // 规则 2 board[x][y] = 'B'; for (int i = 0; i < 8; ++i) { int tx = x + dir_x[i]; int ty = y + dir_y[i]; // 这里不需要在存在 B 的时候继续扩展,因为 B 之前被点击的时候已经被扩展过了 if (tx < 0 || tx >= n || ty < 0 || ty >= m || board[tx][ty] != 'E' || vis[tx][ty]) { continue; } Q[r].x = tx, Q[r++].y = ty; vis[tx][ty] = true; } } } }
char** updateBoard(char** board, int boardSize, int* boardColSize, int* click, int clickSize, int* returnSize, int** returnColumnSizes) { n = boardSize, m = boardColSize[0]; int x = click[0], y = click[1]; if (board[x][y] == 'M') { // 规则 1 board[x][y] = 'X'; } else { bfs(board, x, y); } *returnSize = n; **returnColumnSizes = malloc(sizeof(int*) * n); for (int i = 0; i < n; i++) { (*returnColumnSizes)[i] = m; } return board; }