Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-21-2006, 08:52 AM   #1 (permalink)
priley86
Registered User
 
Join Date: May 2006
Posts: 1
priley86 is on a distinguished road
C++ Sudoku Algorithm

Hi everyone, I have recently written a sudoku algorithm that seems to solve the easy problems. However, in order to solve the most difficult you have to store the board and eventually make guesses, and if the guess is correct, restore the board. I have attempted this towards the bottom of the code, but this is still a work in progress. If anyone has any ideas please let me know. I plan to convert this to a java applet and upload it to a student webpage once I get it working.
Thanks in advance, Patrick.


To test what I have so far:
1. Create a text file called filenumbers in same directory as C++ file. Place your sudoku 9x9 grid similar to this:
0 7 4 0 0 0 6 5 0
5 0 0 6 0 7 0 0 3
3 0 9 0 0 0 2 0 1
0 2 0 4 1 3 0 8 0
0 0 0 8 0 5 0 0 0
0 5 0 2 7 6 0 1 0

2. Copy this code and compile it into an executable.
3. Enjoy

Code:
/*Coded by Patrick Riley... in C++ and C++ alone.Enjoy beating your friends at Sudoku in no time! 
  Last but not least... beware of the ukodus...they might just remember you*/

#include <cstdio>
#include <iostream>
#include <fstream>
int main()
{
    using namespace std;
    ifstream getnum;
    int sudoku[9][9][10];
    int count1,count2,count3,count4,temp,test,trial;
    int remove[9][9];int ukodus[9][9][10];
    int guess[9][9];
    
    /*Get Numbers*/
    getnum.open("filenumbers.txt");
    for(count1=0;count1<9;count1++)
    {
       for(count2=0;count2<9;count2++)
       {
         getnum>>sudoku[count1][count2][9];
       }
    }
    
    /*Output Grid*/
    cout<<"You entered this grid: "<<endl;
    
    for(count1=0;count1<9;count1++)
    {
       for(count2=0;count2<9;count2++)
       {
       cout<<sudoku[count1][count2][9]<<" ";
       }
       cout<<endl;
    }
    
    /*Start Trial to eliminate after possibilites eliminated*/
  for(trial=0;trial<=20;trial++)
  {
    
    /*Store Possibilities*/
       for(count1=0;count1<=8;count1++)
       {
         for(count2=0;count2<=8;count2++)
         {
           if(sudoku[count1][count2][9]==0)
           {
             for(count3=0;count3<=8;count3++)
             {
               sudoku[count1][count2][count3]=count3+1;
             }
           }
           else 
           {
             for(count3=0;count3<=8;count3++)
             {
             sudoku[count1][count2][count3]=0;
             }
           }
          }   
       }
       
       /*eliminate by looking at rows and columns*/
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
          if(sudoku[count1][count2][9]!=0)
          {
            temp=sudoku[count1][count2][9];
            temp--;
            for(count3=0;count3<=8;count3++)
            {
              sudoku[count1][count3][temp]=0;
              sudoku[count3][count2][temp]=0;
            }
          }
        }
      }
    /*eliminate by looking at 3 by 3 squares (this part takes a while). Due to the fact that a 9x9 grid is made of 
      3x3 squares, a pattern between these grids was implemented in the for loops. However, each box of the 3x3 grids 
      must be delt with seperately because of differing patterns.*/
    for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
          if(sudoku[count1][count2][9]!=0)
          {
            temp=sudoku[count1][count2][9];
            temp--;
            
            if(count1%3==0)
            {
              if(count2%3==0)
              {
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1][count2+2][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2+1][temp]=0;
                sudoku[count1+1][count2+2][temp]=0;
                sudoku[count1+2][count2][temp]=0;
                sudoku[count1+2][count2+1][temp]=0;
                sudoku[count1+2][count2+2][temp]=0;
              }
            }
            if(count1%3==0)
            {
              if(count2%3==1)
              {
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2+1][temp]=0;
                sudoku[count1+1][count2-1][temp]=0;
                sudoku[count1+2][count2][temp]=0;
                sudoku[count1+2][count2+1][temp]=0;
                sudoku[count1+2][count2-1][temp]=0;
              }
            }
            if(count1%3==0)
            {
              if(count2%3==2)
              {
                sudoku[count1][count2-2][temp]=0;
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2-1][temp]=0;
                sudoku[count1+1][count2-2][temp]=0;
                sudoku[count1+2][count2][temp]=0;
                sudoku[count1+2][count2-1][temp]=0;
                sudoku[count1+2][count2-2][temp]=0;
              }
            }
            if(count1%3==1)
            {
              if(count2%3==0)
              {
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1][count2+2][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2+1][temp]=0;
                sudoku[count1-1][count2+2][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2+1][temp]=0;
                sudoku[count1+1][count2+2][temp]=0;
              }
            }
            if(count1%3==1)
            {
              if(count2%3==1)
              {
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2+1][temp]=0;
                sudoku[count1-1][count2-1][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2+1][temp]=0;
                sudoku[count1+1][count2-1][temp]=0;
              }
            }
            if(count1%3==1)
            {
              if(count2%3==2)
              {
                sudoku[count1][count2-2][temp]=0;
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1+1][count2][temp]=0;
                sudoku[count1+1][count2-1][temp]=0;
                sudoku[count1+1][count2-2][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2-1][temp]=0;
                sudoku[count1-1][count2-2][temp]=0;
              }
            }
            if(count1%3==2)
            {
              if(count2%3==0)
              {
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1][count2+2][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2+1][temp]=0;
                sudoku[count1-1][count2+2][temp]=0;
                sudoku[count1-2][count2][temp]=0;
                sudoku[count1-2][count2+1][temp]=0;
                sudoku[count1-2][count2+2][temp]=0;
              }
            }
            if(count1%3==2)
            {
              if(count2%3==1)
              {
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1][count2+1][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2+1][temp]=0;
                sudoku[count1-1][count2-1][temp]=0;
                sudoku[count1-2][count2][temp]=0;
                sudoku[count1-2][count2+1][temp]=0;
                sudoku[count1-2][count2-1][temp]=0;
              }
            }
            if(count1%3==2)
            {
              if(count2%3==2)
              {
                sudoku[count1][count2-1][temp]=0;
                sudoku[count1][count2-2][temp]=0;
                sudoku[count1-1][count2][temp]=0;
                sudoku[count1-1][count2-1][temp]=0;
                sudoku[count1-1][count2-2][temp]=0;
                sudoku[count1-2][count2][temp]=0;
                sudoku[count1-2][count2-1][temp]=0;
                sudoku[count1-2][count2-2][temp]=0;
              }
            }
          }
        }
      }
      /*solving after possibilities are removed*/
      
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
          remove[count1][count2]=0;
        }
      }
      
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
           for(count3=0;count3<=8;count3++)
           {
              if(sudoku[count1][count2][count3]!=0)
              {
                remove[count1][count2]++;
              }
           }
        }
      }
      
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
           if(remove[count1][count2]==1)
           {
              for(count3=0;count3<=8;count3++)
              {
                 if(sudoku[count1][count2][count3]!=0)
                 {
                    sudoku[count1][count2][9]=sudoku[count1][count2][count3];                             
                 }
              }
           }
        }
      }
      
 }
      /*Store another array like this one for guessing
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
          for(count3=0;count3<=8;count3++)
          {
            ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
          }
        }
      }  
      
      
      /*Let the guessing begin!! Begin with numbers that only have two possibilities and pick one
      for(count1=0;count1<=8;count1++)
      {
        for(count2=0;count2<=8;count2++)
        {
          if(remove[count1][count2]==2)
          {
            for(count3=0;count3<=8;count3++)
            {
              if(ukodus[count1][count2][count3]!=0)
              {
                ukodus[count1][count2][9]=ukodus[count1][count2][count3];
                guess[count1][count2]=ukodus[count1][count2][count3];
              }
            }
          }
         }
        }  
        /*if we guessed right, solve this and be done with it
        temp=1;
        for(count1=0;count1<=8;count1++)
        {
          for(count2=0;count2<=8;count2++)
          {
           if(udokus[count1][count2][9]==0)
             {
             temp=0;
             }
           }  
         }
         if(temp==1)
         {
            for(count1=0;count1<=8;count1++)
            {
              for(count2=0;count2<=8;count2++)
              {
                for(count3=0;count3<=8;count3++)
                {
                  sudoku[count1][count2][count3]=ukodus[count1][count2][count3];
                }
              }
             }  
          }
      /*If we guessed wrong restore to original
      if(temp==0)
      {
        for(count1=0;count1<=8;count1++)
        {
              for(count2=0;count2<=8;count2++)
              {
                for(count3=0;count3<=8;count3++)
                {
                  ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
                }
              }  
          }
        }
        /*guess another number
        if(temp==0)
        {
          for(count1=0;count1<=8;count1++)
          {
              for(count2=0;count2<=8;count2++)
              {
                for(count3=0;count3<=8;count3++)
                {
                  ukodus[count1][count2][count3]=sudoku[count1][count2][count3];
                }
              }  
          }
        }  
        
      /*break if finished
      if(temp==1)
      {
        break;
      }
      
      
      
      /*Test Row d
       for(count2=0;count2<=8;count2++)
       {
         cout<<" Row: 4 Column: "<<count2+1<<endl;
         for(count3=0;count3<=9;count3++)
         {
           cout<<count3+1<<"="<<sudoku[3][count2][count3]<<",";
         }
         cout<<endl;
       }    */
       cout<<"Answer: "<<endl; 
       /*Output final grid*/
       for(count1=0;count1<9;count1++)
       {
         for(count2=0;count2<9;count2++)
         {
           cout<<sudoku[count1][count2][9]<<" ";
         }
           cout<<endl;
       }
       
    system("Pause");
    return 0;
}

Last edited by redhead; 05-21-2006 at 09:44 AM.
priley86 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use legacy C code in a C# application otakuj462 MS Technologies ( ASP, VB, C#, .NET ) 2 09-22-2005 08:18 PM
Some questions about C fp_unit Standard C, C++ 3 08-14-2005 07:22 PM
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 02:16 PM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 12:03 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting