Official
C - Number Place Editorial by en_translator
Use loop structure like a for statement to check if the given three conditions are all satisfied. To check each condition, for instance one can maintain and update a flag for each digit from \(1\) through \(9\) to manage whether the digit has appeared or not, while scanning the area of the gird, like a row or a column.
For each condition, each cell is referenced at most only once, so even a naive implementation will do. Therefore, the problem has been solved.
Sample code in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[9][9];
int b[9];
bool flag=true;
for(int i=0;i<9;i++)for(int j=0;j<9;j++)cin>>a[i][j];
for(int i=0;i<9;i++){
for(int k=0;k<9;k++)b[k]=0;
for(int j=0;j<9;j++)b[a[i][j]-1]++;
for(int k=0;k<9;k++)if(b[k]!=1)flag=false;
}
for(int j=0;j<9;j++){
for(int k=0;k<9;k++)b[k]=0;
for(int i=0;i<9;i++)b[a[i][j]-1]++;
for(int k=0;k<9;k++)if(b[k]!=1)flag=false;
}
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
for(int k=0;k<9;k++)b[k]=0;
for(int ii=0;ii<3;ii++){
for(int jj=0;jj<3;jj++){
b[a[i+ii][j+jj]-1]++;
}
}
for(int k=0;k<9;k++)if(b[k]!=1)flag=false;
}
}
if(flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
posted:
last update: