修改下面代码,使输出结果格式与预期相同:#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
// 全局变量:棋盘、当前使用的骨牌编号、特殊方格坐标
vector<vector<int>> board;
int tile = 1; // 骨牌编号,从1开始
int specialRow, specialCol;
// 分治法进行棋盘覆盖
void chessCover(int topRow, int topCol, int size, int specialRow, int specialCol) {
if (size == 1) return; // 1x1棋盘无需覆盖
int half = size / 2;
int tileNum = tile++; // 当前使用的骨牌编号
// 判断特殊方格在哪个象限
bool specialInTopLeft = (specialRow < topRow + half && specialCol < topCol + half);
bool specialInTopRight = (specialRow < topRow + half && specialCol >= topCol + half);
bool specialInBottomLeft = (specialRow >= topRow + half && specialCol < topCol + half);
// 默认不在第四象限即为在右下
bool specialInBottomRight = !specialInTopLeft && !specialInTopRight && !specialInBottomLeft;
// 根据特殊方格所在象限,放置中间的L型骨牌
if (!specialInTopLeft) {
board[topRow + half - 1][topCol + half - 1] = tileNum;
}
if (!specialInTopRight) {
board[topRow + half - 1][topCol + half] = tileNum;
}
if (!specialInBottomLeft) {
board[topRow + half][topCol + half - 1] = tileNum;
}
if (!specialInBottomRight) {
board[topRow + half][topCol + half] = tileNum;
}
// 递归处理四个子棋盘
if (specialInTopLeft) {
chessCover(topRow, topCol, half, specialRow, specialCol);
} else {
chessCover(topRow, topCol, half, topRow + half - 1, topCol + half - 1);
}
if (specialInTopRight) {
chessCover(topRow, topCol + half, half, specialRow, specialCol);
} else {
chessCover(topRow, topCol + half, half, topRow + half - 1, topCol + half);
}
if (specialInBottomLeft) {
chessCover(topRow + half, topCol, half, specialRow, specialCol);
} else {
chessCover(topRow + half, topCol, half, topRow + half, topCol + half - 1);
}
if (specialInBottomRight) {
chessCover(topRow + half, topCol + half, half, specialRow, specialCol);
} else {
chessCover(topRow + half, topCol + half, half, topRow + half, topCol + half);
}
}
int main() {
int n;
cin >> n;
cin >> specialRow >> specialCol;
// 初始化棋盘
board.assign(n, vector<int>(n, 0));
board[specialRow][specialCol] = 0; // 特殊格子标记为0
// 开始分治覆盖
chessCover(0, 0, n, specialRow, specialCol);
// 输出结果
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << setw(4) << board[i][j];
}
cout << endl;
}
return 0;
}
最新发布