#include <fstream>#include<vector>#include<iostream>#include<string>using namespacestd;BOOLCheck_sudoku (Constvector<int>&input);intCoordtoidx (intRowintcol);voidExtract_row (Constvector<int>& input,intRow, vector<int>&output);voidExtract_col (Constvector<int>& input,intCol, vector<int>&output);voidExtract_subsquare (Constvector<int>& input,intRowintCol, vector<int>&output);BOOLOnceintNumConstvector<int>&input);intMain () {Ifstream infile; cout<<"Please input a input file name"<<Endl; stringInfilename; CIN>>Infilename; Infile.open (Infilename); if(!Infile.is_open ()) {cout<<"could not open input file"<<Endl; System ("Pause"); Exit (Exit_failure); } Vector<int>input; intNumber ; while(infile>>Number ) {input.push_back (number); } infile.close (); BOOLisok=Check_sudoku (input); if(IsOK) {cout<<"valid"<<Endl; } Else{cout<<"Invalid"<<Endl; } System ("Pause"); return 0;}BOOLCheck_sudoku (Constvector<int>&input) { for(intI=0; i<9; i++) {vector<int>rownumbers; Extract_row (input,i,rownumbers); for(intm=1;m<Ten; m++) { BOOLonceflag=once (m,rownumbers); if(!Onceflag) { return false; } } } for(intj=0; j<9; J + +) {vector<int>colnumbers; Extract_col (input,j,colnumbers); for(intm=1;m<Ten; m++) { BOOLonceflag=once (m,colnumbers); if(!Onceflag) { return false; } } } for(intp=0; p<9; p=p+3) for(intq=0; q<9; q=q+3) {vector<int>subsquarenumbers; Extract_subsquare (input,p,q,subsquarenumbers); for(intm=1;m<Ten; m++) { BOOLonceflag=once (m,subsquarenumbers); if(!Onceflag) { return false; } } } return true;}intCoordtoidx (intRowintCol) { returnrow*9+Col;}voidExtract_row (Constvector<int>& input,intRow, vector<int>&output) { for(intC=0; c<9; C + +) { intidx=Coordtoidx (ROW,C); Output.push_back (input.at (IDX)); }}voidExtract_col (Constvector<int>& input,intCol, vector<int>&output) { for(intR=0; r<9; r++) { intidx=Coordtoidx (R,col); Output.push_back (input.at (IDX)); }}voidExtract_subsquare (Constvector<int>& input,intRowintCol, vector<int>&output) { for(intR=row; r<row+3; r++) for(intC=col; c<col+3; C + +) { intidx=Coordtoidx (R,C); Output.push_back (input.at (IDX)); }}BOOLOnceintNumConstvector<int>&input) { intCount=0; for(unsignedintI=0; I<input.size (); i++) { if(input.at (i) = =num) {Count++; } } returncount==1?true:false;}
Sudoku Detectors: Imperial Engineering C + + jobs