# 11005: 【原1005】数独

### 题目描述

author: 吴杰 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1005

## 说明

1<=n<=20(输入的数独个数）

## Sample Input

``````2
5 8 1 4 9 3 7 6 2
9 6 3 7 1 2 5 8 4
2 7 4 8 6 5 9 3 1
1 2 9 5 4 6 3 7 8
4 3 6 1 8 7 2 9 5
7 5 8 3 2 9 1 4 6
8 9 2 6 7 1 4 5 3
6 1 5 9 3 4 8 2 7
3 4 7 2 5 8 6 1 9

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
``````

## Sample Output

``````Right
Wrong
``````

## BugenZhao's solution

``````//
// Created by BugenZhao on 2019/3/23.
//

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

using ll=long long;

int main() {
int tmp;
bool flag[3][9][9];
int n;
cin >> n;
while (n--) {
bool wrong = false;
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
for (int k = 0; k < 3; ++k) {
flag[k][i][j] = false;
}
}
}
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
scanf("%d", &tmp);
if (wrong) continue;
int k = i / 3 * 3 + j / 3;
if (flag[0][i][tmp] || flag[1][j][tmp] || flag[2][k][tmp]) {
wrong = true;
} else {
flag[0][i][tmp] = flag[1][j][tmp] = flag[2][k][tmp] = true;
}
}
}
if (wrong) printf("Wrong\n");
else printf("Right\n");
}
return 0;
}
``````

## CallMeInk's solution

``````#include <iostream>

using namespace std;

int a[10][10];

int main()
{
int t,a[10][10],i,j;
cin >> t;
for(int tt=1;tt<=t;tt++)
{
bool flag = true;
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
cin >> a[i][j];
for(int i=1;i<=9;i++)
{
int sum1 = 0,sum2 = 1;
for(int j=1;j<=9;j++)
{
sum1 += a[i][j];
sum2 *= a[i][j];
}
if (sum1 != 45 || sum2 != 362880)
{
flag = false;
break;
}
}
for(int j=1;j<=9;j++)
{
int sum1 = 0,sum2 = 1;
for(int i=1;i<=9;i++)
{
sum1 += a[i][j];
sum2 *= a[i][j];
}
if (sum1 != 45 || sum2 != 362880)
{
flag = false;
break;
}
}
for(int i=1;i<=7;i+=3)
for(int j=1;j<=7;j+=3)
{
int sum1 = 0,sum2 = 1;
for (int x=i;x<=i+2;x++)
for(int y=j;y<=j+2;y++)
{
sum1 += a[x][y];
sum2 *= a[x][y];
}
if (sum1 != 45 || sum2 != 362880)
{
flag = false;
break;
}
}
if (flag) cout << "Right" << endl;
else cout << "Wrong" << endl;
}
return 0;
}
``````

## FineArtz's solution

``````/* 数独 */
#include <iostream>
using namespace std;

inline short WhichBlock(int x, int y){
if (x <= 3){
if (y <= 3) return 1;
if (y <= 6) return 2;
if (y <= 9) return 3;
}
if (x <= 6){
if (y <= 3) return 4;
if (y <= 6) return 5;
if (y <= 9) return 6;
}
if (x <= 9){
if (y <= 3) return 7;
if (y <= 6) return 8;
if (y <= 9) return 9;
}
}
int main(){
int k;
cin >> k;
for (int data = 0; data != k; ++data){
bool line[10][10] = {0}, row[10][10] = {0}, block[10][10] = {0}, flag = 1;
short num = 0;
for (int i = 0; i != 9; ++i){
for (int j = 0; j != 9; ++j){
cin >> num;
if (line[i][num] || row[j][num] || block[WhichBlock(i + 1, j + 1)][num]){
flag = 0;
//break;
}
line[i][num] = row[j][num] = block[WhichBlock(i + 1, j + 1)][num] = 1;
}
//if (!flag) break;
}
if (flag) cout << "Right" << endl;
else cout << "Wrong" << endl;
}
return 0;
}
``````

## ligongzzz's solution

``````#include "iostream"
using namespace std;

int main() {
int num;
cin >> num;

for (; num > 0; num--) {
int data[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
cin >> data[i][j];

//Check
bool flag = false;
for (int i = 0; i < 9; i++) {
bool check[9] = { 0 };
for (int j = 0; j < 9; j++) {
if (check[data[i][j] - 1]) {
cout << "Wrong" << endl;
flag = true;
break;
}
check[data[i][j] - 1] = true;
}
if (flag)
break;
}
if (flag)
continue;

//Check
flag = false;
for (int i = 0; i < 9; i++) {
bool check[9] = { 0 };
for (int j = 0; j < 9; j++) {
if (check[data[j][i] - 1]) {
cout << "Wrong" << endl;
flag = true;
break;
}
check[data[j][i] - 1] = true;
}
if (flag)
break;
}
if (flag)
continue;

flag = false;
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
//Small Square
bool check[9] = { 0 };
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
if (check[data[i + m][j + n] - 1]) {
flag = true;
cout << "Wrong" << endl;
goto outside;
}
check[data[i + m][j + n] - 1] = true;
}
}
}
}

outside:
if (flag)
continue;
cout << "Right" << endl;
}

return 0;
}
``````

## Neight99's solution

``````#include <iostream>

using namespace std;

class sudoku {
private:
int **storage;

public:
sudoku();
~sudoku();
bool checkline();
bool checkcolumn();
bool checknine();
friend istream &operator>>(istream &, sudoku &);
};

sudoku::sudoku() {
storage = new int *[9];
for (int i = 0; i < 9; i++) {
storage[i] = new int[9];
for (int j = 0; j < 9; j++) {
storage[i][j] = 0;
}
}
}

sudoku::~sudoku() {
for (int i = 0; i < 9; i++) {
delete[] storage[i];
}
delete[] storage;
storage = NULL;
}

bool sudoku::checkline() {
bool line[9][9] = {0};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int tmp = storage[i][j];
if (line[i][tmp - 1] == 0) {
line[i][tmp - 1] = 1;
} else {
return 0;
}
}
}
return 1;
}

bool sudoku::checkcolumn() {
bool line[9][9] = {0};
for (int j = 0; j < 9; j++) {
for (int i = 0; i < 9; i++) {
int tmp = storage[i][j];
if (line[tmp - 1][j] == 0) {
line[tmp - 1][j] = 1;
} else {
return 0;
}
}
}
return 1;
}

bool sudoku::checknine() {
bool line[9][9] = {0};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int tmp = storage[i][j];
if (line[(i / 3) * 3 + j / 3][tmp - 1] == 0) {
line[(i / 3) * 3 + j / 3][tmp - 1] = 1;
} else {
return 0;
}
}
}
return 1;
}

istream &operator>>(istream &is, sudoku &s) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
is >> s.storage[i][j];
}
}
return is;
}

int main() {
int n;
sudoku *su;

cin >> n;
su = new sudoku[n];
for (int i = 0; i < n; i++) {
cin >> su[i];
}

for (int i = 0; i < n; i++) {
bool check =
(su[i].checkline() && su[i].checkcolumn() && su[i].checknine());
if (check == 1) {
cout << "Right" << endl;
} else {
cout << "Wrong" << endl;
}
}

return 0;
}
``````

## satgo1546's solution

``````#include <iostream>
using namespace std;

// 要检查的方格组。
const int f[3][2][9] = {
{
{0, 9, 18, 27, 36, 45, 54, 63, 72},
{0, 1, 2, 3, 4, 5, 6, 7, 8},
}, {
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{0, 9, 18, 27, 36, 45, 54, 63, 72},
}, {
{0, 3, 6, 27, 30, 33, 54, 57, 60},
{0, 1, 2, 9, 10, 11, 18, 19, 20},
}
};
int a[81];

int main(int argc, char *argv[]) {
cin >> argc;
while (argc--) {
bool b = true;
for (int i = 0; i < 81; i++) cin >> a[i];
for (int i = 0; i - 3; i++) {
for (int j = 0; j - 9; j++) {
// 检查方格组中的数字有无重复。
int t = 0;
for (int k = 0; k - 9; k++) t ^= 1 << a[f[i][0][j] + f[i][1][k]];
if (t - 1022) b = false;
}
}
cout << (b ? "Right" : "Wrong") << endl;
}
return 0;
}
``````

## skyzh's solution

``````#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

bool verify(bool data[10]) {
for (int i = 1; i <= 9; i++) if (!data[i]) return false;
return true;
}

void run() {
int D[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
scanf("%d", &D[i][j]);
}
}
bool ok = true;
for (int i = 0; i < 9; i++) {
bool data[10] = { 0 };
for (int j = 0; j < 9; j++) {
data[D[i][j]] = true;
}
ok &= verify(data);
}
for (int i = 0; i < 9; i++) {
bool data[10] = { 0 };
for (int j = 0; j < 9; j++) {
data[D[j][i]] = true;
}
ok &= verify(data);
}
for (int R = 0; R < 3; R++) {
for (int C = 0; C < 3; C++) {
bool data[10] = { 0 };
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
data[D[R*3+x][C*3+y]] = true;
}
}
ok &= verify(data);
}
}
if (ok) printf("Right\n"); else printf("Wrong\n");
}

int main() {
int N;
scanf("%d", &N);
for (int t = 0; t < N; t++) run();
return 0;
}
``````

## yyong119's solution

``````#include <iostream>
int n,i,j,k,l;
int a[10][10],c[10];
bool te;
int main(){
using namespace std;
cin>>n;
while(n){
te=false;
for (i=1; i<=9; i++){
for (j=1; j<=9; j++) c[j]=0;
for (j=1; j<=9; j++){
cin>>a[i][j];
c[a[i][j]]++;
}
for (j=1; j<=9; j++) if (c[j]>1) te=true;
}
for (i=1; i<=3; i++)
for (j=1; j<=3; j++){
for (k=1; k<=9; k++) c[k]=0;
for (k=3*i-2; k<=3*i; k++)
for (l=3*j-2; l<=3*j; l++)
c[a[k][l]]++;
for (k=1; k<=9; k++) if (c[k]>1) te=true;
}
if (te) cout<<"Wrong\n";else cout<<"Right\n";
n--;
}
return 0;
}
``````