Background XX School swept a mental game, that is, Sudoku (nine Gongge), first give you a Sudoku, and need you to verify whether the rules. Describe the following specific rules:
Each line is used to 1,2,3,4,5,6,7,8,9, the position is not limited,
Each column is used in the 1,2,3,4,5,6,7,8,9, the location is not limited,
Each 3x3 lattice (a total of nine such lattices) is used to 1,2,3,4,5,6,7,8,9, the position is not limited,
The process of the game is to fill the blanks with 1,2,3,4,5,6,7,8,9, and requirements for each row, each column, each nine Gongge used to 1,2,3,4,5,6,7,8,9.
The following is a correct sudoku:
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
Input format input n Sudoku, you can verify if it violates the rules.
The number of first acts Sudoku, the second line begins with the first Sudoku, then the second, to Nth.
Attention! There is a carriage return between each sudoku! output format if correct output "right" if incorrect then output "wrong" output a line test sample 1 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
Output
Right
Wrong
Note 1<=n<=20 (number of Sudoku inputs)
Regardless of whether the input Sudoku is wrong or correct, the data guarantees that each number is between 1-9, which means that the rule is violated only because there are the same number, and not because the number exceeds the 1-9 range. Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 inta[Ten][Ten],n;7 intHengxiang (inti) {8 intyanzheng[ One]={0};9 for(intk=1; k<=9; k++) Ten if(yanzheng[a[i][k]]==0) yanzheng[a[i][k]]=1; One Else{/*cout<< "Hengxiang" <<i<< "" <<k<<endl;*/return-1;} A return 0; - } - the intShuxiang (inti) { - intyanzheng[ One]={0}; - for(intk=1; k<=9; k++) - if(yanzheng[a[k][i]]==0) yanzheng[a[k][i]]=1; + Else{/*cout<< "Shuxiang" <<i<< "" <<k<<endl;*/return-1;} - return 0; + } A at intJiugong (intIintj) { - intyanzheng[ One]={0}; - for(intk1=3*i+1; k1<=3*i+3; k1++){ - for(intK2=3*j+1; k2<=3*j+3; k2++){ - //cout<<a[k1][k2]<< ""; - if(yanzheng[a[k1][k2]]==0) yanzheng[a[k1][k2]]=1; in Else{/*cout<< "Jiugong i=" <<i<< "j=" <<j<< "<<k1<<" <<k2<<endl;*/return-1;} - } to //cout<<endl; + } - //cout<<endl; the return 0; * } $ Panax Notoginseng intMain () { - //freopen ("01.txt", "R", stdin); thescanf"%d",&N); + for(ints=1; s<=n;s++){ AMemset (A,0,sizeof(a)); the for(intI=1; i<=9; i++){ + for(intj=1; j<=9; j + +){ -scanf"%d",&a[i][j]); $ } $ } - - intflag=1; the - if(flag==1) for(intI=1; i<=9; i++)if(Hengxiang (i) ==-1) {flag=-1; Break;}Wuyi if(flag==1) for(intI=1; i<=9; i++)if(Shuxiang (i) ==-1) {flag=-1; Break;} the if(flag==1) for(intI=0;i<3; i++) { - if(flag==1) Wu for(intj=0;j<3; j + +){ - if(Jiugong (I,J) ==-1) {flag=-1; Break;} About } $ } - if(flag==1) cout<<" Right"<<Endl; - Elsecout<<"wrong"<<Endl; - } A return 0; +}
Open array Marker 1~9
If the current block is marked
Then all transverse scan vertical Scan nine Gongge scan all break
Total call Vertical scan 9 times
Transverse 9 times
Nine Gongge Scan 9 times
TYVJ P1093 Verification Sudoku Label:none