Files
2026-01-31 14:38:00 +08:00

74 lines
1.8 KiB
C++

#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main(){
int itemize = 0; // 定义必要变量
int a[4][4];
deque<pair<int, int>> sp;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cin >> a[i][j];
if(a[i][j] == 0){
sp.push_back({i, j});
}
}
}
while(!sp.empty()){
pair<int, int> point = sp.front();
sp.pop_front();
// 记录当前格子 (point.first, point.second) 哪些数字不能填
bool used[5] = {false};
// 1. 检查行
for(int i = 0; i < 4; i++){
if(a[point.first][i] != 0) used[a[point.first][i]] = true;
}
// 2. 检查列
for(int i = 0; i < 4; i++){
if(a[i][point.second] != 0) used[a[i][point.second]] = true;
}
// 3. 检查 2x2 子网格
int rs = (point.first / 2) * 2, cs = (point.second / 2) * 2;
for(int i = rs; i < rs + 2; i++){
for(int j = cs; j < cs + 2; j++){
if(a[i][j] != 0) used[a[i][j]] = true;
}
}
int can_fill_count = 0;
int fill_val = 0;
for(int v = 1; v <= 4; v++){
if(!used[v]){
can_fill_count++;
fill_val = v;
}
}
if(can_fill_count == 1) {
// 如果唯一确定,填入数字
a[point.first][point.second] = fill_val;
} else {
// 暂时无法确定,放回队尾等下一轮
sp.push_back(point);
}
}
// 输出结果,注意题目要求的格式
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cout << a[i][j] << (j == 3 ? "" : " ");
}
cout << endl;
}
return 0;
}