标签:
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
Right
Wrong
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[10][10],N; 7 int hengxiang(int i){ 8 int yanzheng[11]={0}; 9 for(int k=1;k<=9;k++) 10 if(yanzheng[a[i][k]]==0) yanzheng[a[i][k]]=1; 11 else {/*cout<<"hengxiang"<<i<<" "<<k<<endl;*/return -1;} 12 return 0; 13 } 14 15 int shuxiang(int i){ 16 int yanzheng[11]={0}; 17 for(int k=1;k<=9;k++) 18 if(yanzheng[a[k][i]]==0) yanzheng[a[k][i]]=1; 19 else {/*cout<<"shuxiang"<<i<<" "<<k<<endl;*/return -1;} 20 return 0; 21 } 22 23 int jiugong(int i,int j){ 24 int yanzheng[11]={0}; 25 for(int k1=3*i+1;k1<=3*i+3;k1++){ 26 for(int k2=3*j+1;k2<=3*j+3;k2++){ 27 // cout<<a[k1][k2]<<" "; 28 if(yanzheng[a[k1][k2]]==0) yanzheng[a[k1][k2]]=1; 29 else {/*cout<<"jiugong i="<<i<<" j="<<j<<" "<<k1<<" "<<k2<<endl;*/return -1;} 30 } 31 // cout<<endl; 32 } 33 // cout<<endl; 34 return 0; 35 } 36 37 int main(){ 38 // freopen("01.txt","r",stdin); 39 scanf("%d",&N); 40 for(int s=1;s<=N;s++){ 41 memset(a,0,sizeof(a)); 42 for(int i=1;i<=9;i++){ 43 for(int j=1;j<=9;j++){ 44 scanf("%d",&a[i][j]); 45 } 46 } 47 48 int flag=1; 49 50 if(flag==1) for(int i=1;i<=9;i++) if(hengxiang(i)==-1) {flag=-1;break;} 51 if(flag==1) for(int i=1;i<=9;i++) if(shuxiang(i)==-1) {flag=-1;break;} 52 if(flag==1) for(int i=0;i<3;i++) { 53 if(flag==1) 54 for(int j=0;j<3;j++){ 55 if(jiugong(i,j)==-1) {flag=-1;break;} 56 } 57 } 58 if(flag==1) cout<<"Right"<<endl; 59 else cout<<"Wrong"<<endl; 60 } 61 return 0; 62 }开数组标记1~9
若当前块被标记过了
则接下来所有横向扫描 竖向扫描 九宫格扫描都break
总共调用竖向扫描9次
横向9次
九宫格扫描9次
标签:
原文地址:http://www.cnblogs.com/radiumlrb/p/5781107.html