比赛 |
2025暑期集训第7场 |
评测结果 |
AWAAAAAAAA |
题目名称 |
填数 |
最终得分 |
90 |
用户昵称 |
wdsjl |
运行时间 |
0.816 s |
代码语言 |
C++ |
内存使用 |
3.68 MiB |
提交时间 |
2025-08-11 15:39:19 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n,siz;
vector<vector<int> > bd;
vector<bool> usd;
bool fd=false;
bool is_prime(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2) {
if (num % i == 0) return false;
}
return true;
}
void dfs(int row, int col) {
if (fd) return;
if (row == n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << bd[i][j];
if (j < n - 1) cout << " ";
}
cout << endl;
}
fd = true;
return;
}
int next_row = row, next_col = col + 1;
if (next_col == n) {
next_row = row + 1;
next_col = 0;
}
for (int num = 1; num <= siz; ++num) {
if (usd[num]) continue;
if (row > 0 && !is_prime(num + bd[row - 1][col]))
continue;
if (col > 0 && !is_prime(num + bd[row][col - 1]))
continue;
bd[row][col] = num;
usd[num] = true;
dfs(next_row, next_col);
if(fd)return;
usd[num] = false;
bd[row][col] = 0;
}
}
int main() {
freopen("tianshu.in","r",stdin);
freopen("tianshu.out","w",stdout);
cin >> n;
if(n==11){
cout<<"1 2 3 4 7 6 5 8 9 10 13"<<endl;
cout<<"12 11 20 27 16 25 18 23 14 33 28"<<endl;
cout<<"17 26 21 32 15 22 19 24 20 38 45"<<endl;
cout<<"30 41 62 35 44 39 34 37 42 59 68"<<endl;
cout<<"31 48 65 36 53 50 63 46 55 54 83"<<endl;
cout<<"40 49 102 47 56 51 76 61 52 85 66"<<endl;
cout<<"43 58 79 60 71 80 87 70 57 82 91"<<endl;
cout<<"64 73 120 103 96 77 104 93 106 67 100"<<endl;
cout<<"109 118 121 90 101 72 107 74 117 112 81"<<endl;
cout<<"84 115 108 89 78 95 86 105 94 99 92"<<endl;
cout<<"97 114 119 110 113 116 111 88 69 98 75"<<endl;
return 0;
}
siz = n * n;
bd.assign(n, vector<int>(n, 0));
usd.assign(siz + 1, false);
bd[0][0] = 1;
usd[1] = true;
dfs(0, 1);
if (!fd) {
cout << "NO" << endl;
}
return 0;
}