陈志轩 • 7个月前
rt,我发现这题并没有SPJ
这段代码交上去:
#include<bits/stdc++.h>
using namespace std;
int res[5][5],ans;
bool f[20];
void dfs(int x,int y){
if (y > 3){
dfs(x + 1,1);
return ;
}
if (x > 3){
int sum = res[1][1] + res[1][2] + res[1][3];
for (int i = 2;i <= 3;i++){
if (res[i][1] + res[i][2] + res[i][3] != sum){
return ;
}
}
for (int i = 1;i <= 3;i++){
if (res[1][i] + res[2][i] + res[3][i] != sum){
return ;
}
}
if ((res[1][1] + res[2][2] + res[3][3] != sum) || (res[1][3] + res[2][2] + res[3][1]) != sum){
return ;
}
for (int i = 1;i <= 3;i++){
for (int j = 1;j <= 3;j++){
cout<<res[i][j];
}
putchar('\n');
}
putchar('\n');
ans++;
return ;
}
for (int i = 1;i <= 9;i++){
if (f[i]){
continue;
}
res[x][y] = i;
f[i] = true;
dfs(x,y + 1);
f[i] = false;
}
}
int main(){
dfs(1,1);
cout<<ans;
return 0;
}
最终输出只是与标准答案顺序不同,方案数是相同的
输出:
276
951
438
294
753
618
438
951
276
492
357
816
618
753
294
672
159
834
816
357
492
834
159
672
8
答案:
618
753
294
816
357
492
672
159
834
834
159
672
276
951
438
438
951
276
294
753
618
492
357
816
8
而题目中说了方案不分先后,所以应该添加SPJ。
评论: