Skip to content

Instantly share code, notes, and snippets.

@projectxcappe
Created September 6, 2011 16:49
Show Gist options
  • Save projectxcappe/1198140 to your computer and use it in GitHub Desktop.
Save projectxcappe/1198140 to your computer and use it in GitHub Desktop.
Sudoku to cnf solver
//Cass Pangell
//9.23.2008
//sudoku2cnf rewrite
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
int main(){
int r, c, i, j, N = 0;
int v, M, P, a =1;
/*
//------------------INDIVIDUAL CELL CLAUSES--------------//
for(r=1; r<10; r++)
{
for(c=1; c<10; c++)
{
for(v=1; v<10; v++)
{
N = r*100 + c*10 + v;
cout<<N<<" ";
}//for v
cout<<"0"<<endl;
}//for c
}//for r
cout<<endl;
//INDIVIDUAL CELL PAIRS
for(r=1; r<10; r++)
{
for(c=1; c<10; c++)
{
for(v=1; v<10; v++)
{
for(j=(v+1); j<10; j++)
{
cout<<-r<<c<<v<<" ";
cout<<-r<<c<<j<<" 0"<<endl;
}
}
cout<<endl;
}
}
//----------------------ROW CLAUSES---------------------//
for(r=1; r<10; r++)
{
for(v=1; v<10; v++)
{
for(c=1; c<10; c++)
{
M = r*100 + c*10 + v;
cout<<M<<" ";
}
cout<<"0"<<endl;
}
}
//INDIVIDUAL ROW PAIRS
for(r=1; r<10; r++)
{
for(c=1; c<10; c++)
{
cout<<-r<<r<<r<<" ";
cout<<-r<<c<<r<<" 0"<<endl;
}
cout<<endl;
}
//---------------------COLUMN CLAUSES--------------------//
for(v=1; v<10; v++)
{
for(c=1; c<10; c++)
{
for(r=1; r<10; r++)
{
M = r*100 + c*10 + v;
cout<<M<<" ";
}
cout<<"0"<<endl;
}
}
//INDIVIDUAL COLUMN PAIRS
for(r=1; r<10; r++)
{
for(c=1; c<10; c++)
{
cout<<-r<<r<<r<<" ";
cout<<-c<<r<<r<<" 0"<<endl;
}
cout<<endl;
}
*/
//--------------------BLOCK CLAUSES---------------------//
//BLOCK 1 (1,1) THRU (3,3)
for(N=1; N<10; N++)
{
for(r=1; r<4; r++)
{
for(c=1; c<4; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 2 (1,4) THRU (3,6)
for(N=1; N<10; N++)
{
for(r=1; r<4; r++)
{
for(c=4; c<7; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 3 (1,7) THRU (3,9)
for(N=1; N<10; N++)
{
for(r=1; r<4; r++)
{
for(c=7; c<10; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 4 (4,1) THRU (6,3)
for(N=1; N<10; N++)
{
for(r=4; r<7; r++)
{
for(c=1; c<4; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 5 (4,4) THRU (6,6)
for(N=1; N<10; N++)
{
for(r=4; r<7; r++)
{
for(c=4; c<7; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 6 (4,7) THRU (6,9)
for(N=1; N<10; N++)
{
for(r=4; r<7; r++)
{
for(c=7; c<10; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 7 (7,1) THRU (9,3)
for(N=1; N<10; N++)
{
for(r=7; r<10; r++)
{
for(c=1; c<4; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 8 (7,4) THRU (9,6)
for(N=1; N<10; N++)
{
for(r=7; r<10; r++)
{
for(c=4; c<7; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//BLOCK 9 (7,7) THRU (9,9)
for(N=1; N<10; N++)
{
for(r=7; r<10; r++)
{
for(c=7; c<10; c++)
{
cout<<r<<c<<N<<" ";
}
}
cout<<" 0"<<endl;
}
cout<<endl;
//-------------------------READ IN FILE = PREFILLED CELLS---------//
ifstream inFile;
int array[8][8];
inFile.open("sudoku_input");
if(!inFile.is_open()){cout<<"Can't open file"; exit(1);}
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
inFile>>array[i][j];
cout<<array[i][j];
}
cout<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment