Skip to content

Instantly share code, notes, and snippets.

@guptarohit
Last active November 3, 2016 06:18
Show Gist options
  • Save guptarohit/6d92b055863a40fa14ce187b812bc5b4 to your computer and use it in GitHub Desktop.
Save guptarohit/6d92b055863a40fa14ce187b812bc5b4 to your computer and use it in GitHub Desktop.
Program to check the numbers possible at a position of a Sudoku.
//This program checks the numbers possible at a position of a Sudoku.
//@author : Rohit Gupta
#include <iostream>
using namespace std;
int main() {
int i, j, p, q, ch = 1, freq[10] = {0};
int s[9][9] = {
{0, 0, 0, 0, 0, 0, 6, 8, 0},
{0, 0, 0, 0, 7, 3, 0, 0, 9},
{3, 0, 9, 0, 0, 0, 0, 4, 5},
{4, 9, 0, 0, 0, 0, 0, 0, 0},
{8, 0, 3, 0, 5, 0, 9, 0, 2},
{0, 0, 0, 0, 0, 0, 0, 3, 6},
{9, 6, 0, 0, 0, 0, 3, 0, 8},
{7, 0, 0, 6, 8, 0, 0, 0, 0},
{0, 2, 8, 0, 0, 0, 0, 0, 0}
};
while(ch==1) {
cout<<"Enter positions(row, col): ";
cin>>i>>j;
if(i<1 || i>9 || j<1 || j>9) {
cout<<"\nErr! Please enter values in range(i.e within 1 and 9). Try again!\n"<<endl;
continue;
}
i--; j--;
if(s[i][j]>0) {
cout<<"\nErr! This position is already filled. Try Again!\n"<<endl;
continue;
}
for(p=0; p<9; p++){
if(p==i) continue;
freq[s[p][j]]++;
}
for(q=0; q<9; q++) {
if(q==j) continue;
freq[s[i][q]]++;
}
for(p=i-(i-1)%3; p<= i+(3-i%3)%3; p++) {
for(q=i-(i-1)%3; q<= i+(3-i%3)%3; q++) {
if(p==i && q==j) continue;
freq[s[p-1][q-1]]++;
}
}
cout<<"\nPossible numbers on position ("<<++i<<", "<<++j<<") is: ";
for(p=1; p<10; p++) {
if(freq[p]<1){
cout<<p<<" ";
}
}
cout<<"\n\nTo check again press '1' or anything to quit.: ";
cin>>ch;
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment