Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,673 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,177 people online right now. Registration is fast and FREE... Join Now!




Problem with sudoku solver

 
Reply to this topicStart new topic

Problem with sudoku solver

Pontus
post 2 Sep, 2007 - 01:25 AM
Post #1


Dreaming Coder / Coding Dreamer

Group Icon
Joined: 28 Dec, 2006
Posts: 527



Thanked 2 times

Dream Kudos: 275
My Contributions


Ok, i am working on a sudoku solver. It works right with only a few little mistakes that cause him to not be able to finish the puzzle, Projects is in 3 files:

main.cpp:
CODE

#include <iostream>
#include "sudoku.h"

using namespace std;

int data[9][9]={
               {0,0,0, 3,0,8, 0,9,6},
               {0,3,0, 0,0,0, 0,2,8},
               {9,0,0, 0,2,0, 0,0,3},
              
               {0,0,7, 9,0,0, 0,0,0},
               {0,0,0, 6,8,1, 0,0,0},
               {0,0,0, 0,0,7, 3,0,0},
              
               {6,0,0, 0,9,0, 0,0,5},
               {1,4,0, 0,0,0, 0,7,0},
               {7,9,0, 1,0,3, 0,0,0}};

int main()
{
sudoku sudoku1;
for(int a=0;a<9;a++)
{
  for(int b=0;b<9;b++)
  {
   sudoku1.data[a][b]=data[a][b];
  }
}
sudoku1.check_row(5);
bool run=true;
string contin;
while(run)
{
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   sudoku1.slice_dice(a*3,b*3);
  }
}
for(int a=0;a<9;a++)
{
  sudoku1.check_row(a);
}
for(int a=0;a<9;a++)
{
  sudoku1.check_col(a);
}
for(int a=0;a<9;a++)
{
  if(a==0)
   cout<<"\n-------------\n";
  for(int b=0;b<9;b++)
  {
   if(b==0||b==3||b==6)
    cout<<"|";
   if(sudoku1.data[a][b]!=0)
    cout<<sudoku1.data[a][b];
   else
    cout<<" ";
   if(b==8)
    cout<<"|";
  }
  if(a==2||a==5||a==8)
   cout<<"\n-------------";
  cout<<"\n";
}

  cin>>contin;
  if(contin=="stop")
   run=false;
}
cin.sync();
cin.get();
return 0;
}

sudoku.h
CODE
#pragma once
#ifndef SUDOKU_H
#define SUDOKU_H
class sudoku
{
public:
        int data[9][9];
        sudoku();
        void slice_dice(int y, int x);
        void check_row(int y);
        void check_col(int x);
};
#endif

sudoku.cpp
CODE

#include "sudoku.h"
#include <vector>
#include <iostream>

using namespace std;

sudoku::sudoku()
{
for(int a=0;a<9;a++)
{
  for(int b=0;b<9;b++)
  {
   data[a][b]=0;
  }
}
data[0][0]=1;
data[1][0]=2;
data[2][0]=3;
data[3][0]=4;
data[4][0]=5;
data[5][0]=6;
data[6][0]=7;
data[8][0]=8;
}

void remove(vector<int> *x,vector<int> *y,int get_y,int get_x,bool isy)
{
for(int a=0;a<x->size();a++)
{
  if(isy)
  {
   if(y->at(a)==get_y)
   {
    x->erase(x->begin()+a);
    y->erase(y->begin()+a);
    a--;
   }
  }
  else
  {
   if(x->at(a)==get_x)
   {
    x->erase(x->begin()+a);
    y->erase(y->begin()+a);
    a--;
   }
  }
}
}
void remove(vector<int> *test,int get_test)
{
for(int a=0;a<test->size();a++)
{
   if(test->at(a)==get_test)
   {
    test->erase(test->begin()+a);
    a--;
   }
}
}

void sudoku::slice_dice(int y, int x)
{
vector<int>possible_x[9];
vector<int>possible_y[9];
bool present[9]={true,true,true,true,true,true,true,true,true};
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[a+y][b+x]!=0)
    present[data[a+y][b+x]-1]=false;
  }
}
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<3;b++)
   {
    for(int c=0;c<3;c++)
    {
     if(data[b+y][c+x]==0)
     {
      possible_x[a].push_back(y+c);
      possible_y[a].push_back(x+b);
     }
    }
   }
  }
}
//slice
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    if(data[y][b]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],y,0,1);
    }
    if(data[y+1][b]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],y+1,0,1);
    }
    if(data[y+2][b]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],y+2,0,1);
    }
   }
  }
}
//dice
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    if(data[b][x]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],0,x,0);
    }
    if(data[b][x+1]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],0,x+1,0);
    }
    if(data[b][x+2]==(a+1))
    {
     remove(&possible_x[a],&possible_y[a],0,x+2,0);
    }
   }
  }
}
for(int a=0;a<9;a++)
{
  if(possible_y[a].size()==1)
  {
   if(data[possible_y[a].at(0)][possible_x[a].at(0)]==0)
    data[possible_y[a].at(0)][possible_x[a].at(0)]=a+1;
  }
}
}
void sudoku::check_col(int x)
{
vector<int>possible_y[9];
bool present[9]={true,true,true,true,true,true,true,true,true};
for(int a=0;a<9;a++)
{
  if(data[a][x]!=0)
   present[data[a][x]-1]=false;
}
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    if(data[b][x]==0)
     possible_y[a].push_back(b);
   }
  }
}
//remove by dice
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    for(int c=0;c<9;c++)
    {
     if(data[b][c]==(a+1))
     {
      remove(&possible_y[a],b);
     }
    }
   }
  }    
}
int place;
if(x==0||x==3||x==6)
  place=0;
if(x==1||x==4||x==7)
  place=1;
if(x==2||x==5||x==8)
  place=2;
//remove by already in square
bool up[9]={false,false,false,false,false,false,false,false,false};
bool mid[9]={false,false,false,false,false,false,false,false,false};
bool down[9]={false,false,false,false,false,false,false,false,false};
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[b][x+a-place]!=0)
    up[data[b][x+a-place]-1]=true;
  }
}
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[b+3][x+a-place]!=0)
    mid[data[b+3][x+a-place]-1]=true;
  }
}
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[b+6][x+a-place]!=0)
    down[data[b+6][x+a-place]-1]=true;
  }
}
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   if(up[a])
   {
    remove(&possible_y[a],0);
    remove(&possible_y[a],1);
    remove(&possible_y[a],2);
   }
   if(mid[a])
   {
    remove(&possible_y[a],3);
    remove(&possible_y[a],4);
    remove(&possible_y[a],5);
   }
   if(down[a])
   {
    remove(&possible_y[a],6);
    remove(&possible_y[a],7);
    remove(&possible_y[a],8);
   }
  }
}
//check
for(int a=0;a<9;a++)
{
  if(possible_y[a].size()==1)
  {
   if(data[possible_y[a].at(0)][x]==0)
    data[possible_y[a].at(0)][x]=a+1;
  }
}
}
void sudoku::check_row(int y)
{
vector<int>possible_x[9];
bool present[9]={true,true,true,true,true,true,true,true,true};
for(int a=0;a<9;a++)
{
  if(data[y][a]!=0)
   present[data[y][a]-1]=false;
}
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    if(data[y][b]==0)
     possible_x[a].push_back(b);
   }
  }
  cout<<"possible_x.size="<<possible_x[a].size()<<endl;
}
//remove by dice
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   for(int b=0;b<9;b++)
   {
    for(int c=0;c<9;c++)
    {
     if(data[c][b]==(a+1))
     {
      remove(&possible_x[a],b);
     }
    }
   }
  }
  cout<<"possible_x.size="<<possible_x[a].size()<<endl;    
}
int place;
if(y==0||y==3||y==6)
  place=0;
if(y==1||y==4||y==7)
  place=1;
if(y==2||y==5||y==8)
  place=2;
cout<<"place="<<place<<endl;
  cout<<"In square"<<endl;
//remove by already in square
bool left[9]={false,false,false,false,false,false,false,false,false};
bool mid[9]={false,false,false,false,false,false,false,false,false};
bool right[9]={false,false,false,false,false,false,false,false,false};
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[y+a-place][b]!=0)
    left[data[y+a-place][b]-1]=true;
  }
}
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[y+a-place][b+3]!=0)
    mid[data[y+a-place][b+3]-1]=true;
  }
}
for(int a=0;a<3;a++)
{
  for(int b=0;b<3;b++)
  {
   if(data[y+a-place][b+6]!=0)
    right[data[y+a-place][b+6]-1]=true;
  }
}
for(int a=0;a<9;a++)
{
  if(present[a])
  {
   if(left[a])
   {
    remove(&possible_x[a],0);
    remove(&possible_x[a],1);
    remove(&possible_x[a],2);
   }
   if(mid[a])
   {
    remove(&possible_x[a],3);
    remove(&possible_x[a],4);
    remove(&possible_x[a],5);
   }
   if(right[a])
   {
    remove(&possible_x[a],6);
    remove(&possible_x[a],7);
    remove(&possible_x[a],8);
   }
  }
}
//check
for(int a=0;a<9;a++)
{
  cout<<"possible_x.size="<<possible_x[a].size()<<endl;
  if(possible_x[a].size()==1)
  {
   if(data[y][possible_x[a].at(0)]==0)
    data[y][possible_x[a].at(0)]=a+1;
  }
}
}


pls help me because everything looks allright for me
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 2 Sep, 2007 - 01:42 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


what problems are you having, or are there errors?
Please be specific about the problem.
User is offlineProfile CardPM

Go to the top of the page

Pontus
post 2 Sep, 2007 - 02:18 AM
Post #3


Dreaming Coder / Coding Dreamer

Group Icon
Joined: 28 Dec, 2006
Posts: 527



Thanked 2 times

Dream Kudos: 275
My Contributions


He enters wrong numbers in the wrong places
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month