locked
program to solve sudoku RRS feed

  • Question

  • come up with the ideas to get the classic sudoku puzzles solved by a computer program?
    Saturday, May 26, 2007 2:23 PM

Answers

  • yeah every1 has to.
    think about artificial intelligence.
    Sunday, May 27, 2007 3:00 AM
  • hey everybody got puzzled or hibernating?
    i have given you a hint.
    apply AI technique.
    if you don't know what sudoku is then goto

    www.sudoku.com
    Sunday, May 27, 2007 6:43 AM
  • I have coded a program for sudoku solving.. Those of you who want the program, mail me at raghuramdcbe@gmail.com  and i'll mail you the code.. My program is in c language.. And one request, while you mail , give a few details about you so that i shall know about you guys..
    Monday, May 28, 2007 2:10 AM
  • Hi friends I made an application in .NET which is same as SUDOKU
    Monday, May 28, 2007 5:38 AM
  • post here.
    that's why i started this thread.
    Monday, May 28, 2007 6:23 AM
  • that's nice budy.
    all i want to see is LOGIC
    whichever language you use doesn't matter much
    Monday, May 28, 2007 6:25 AM
  • Code Snippet : SODOKU Solver

     

    THIS CODE WORKS IN TC ONLY !!! 

     

    #include<stdio.h>


    int board[9][9],m=1,s=1;

     

    void intro()
    {
     clrscr();
     gotoxy(30,5);
     printf("Sudoku Solver\n\n\n");
     printf("\n\n\t\tProgrammer: Raghuram Duraisamy\n\n");
     printf("\t\tE-mail Address : Raghuramdcbe@gmail.com");
     getch();
    }

     

    main()
    {
     intro();
     print();
     input();
     gotoxy(20,23);
     printf("PLEASE WAIT ");
     solver();
     if(s==1)
      printf("\n\nNo solution exists\nPress any key to exit");
     getch();
     return;
    }

     

    solver()
    {
     static int num1[9][9];
     int i,j,num[1][12]={0},n=0;
     for(i=0;i<9;i++)
      for(j=0;j<9;j++)
       num1[i][j]=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(board[i][j]!=0)
        n++;
      }
     }
     if(n==81)
     {
      m=2;
      s=2;
      print();
      getch();
      clrscr();
      printf("\n\n\n\n\n\t\t\t\tBYE!BYE!");
      getch();
      exit();
     }
     else
     {
      findNoNum(num1);
      n=findNum(num1,num);
      i=0;
      if(n>0)
      {
       for(j=3;j<3+num[i][2];j++)
       {
        board[num[i][0]][num[i][1]]=num[i][j];
        solver();
       }
       board[num[i][0]][num[i][1]]=0;
      }
      }
      return;
    }


    findNoNum(int num1[][9])
    {
     int check1;
     int i,j,k;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(board[i][j]==0)
       {
        for(k=1;k<=9;k++)
        {
         check1=possibility(i,j,k);
         if(check1==1)
         {
          num1[i][j]++;
         }
        }
       }
      }
     }
     return;
    }


    possibility(int i,int j,int k)
    {
     int l,i_s,i_e,j_s,j_e,m;
     int check1=1;
     for(l=0;l<9;l++)
     {
      if(board[i][l]==k)
      {
       check1=0;
       break;
      }
     }
     if(check1==0)
      return(check1);
     for(l=0;l<9;l++)
     {
      if(board[l][j]==k)
      {
       check1=0;
       break;
      }
     }
     if(check1==0)
      return(check1);
     switch(i/3)
     {
      case 0:
       i_s=0;
       i_e=2;
       break;
      case 1:
       i_e=5;
       i_s=3;
       break;
      case 2:
       i_s=6;
       i_e=8;
       break;
     }
     switch(j/3)
     {
      case 0:
       j_s=0;
       j_e=2;
       break;
      case 1:
       j_e=5;
       j_s=3;
       break;
      case 2:
       j_s=6;
       j_e=8;
       break;
     }
     for(l=i_s;l<=i_e;l++)
     {
      for(m=j_s;m<=j_e;m++)
      {
       if(board[l][m]==k)
       {
        check1=0;
        break;
       }
      }
      if(check1==0)
       break;
     }
     return(check1);
    }


    findNum(int num1[9][9],int num[][12])
    {
     int i,j,k=0,small,l=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]!=0)
       {
        small=num1[i][j];
        l=1;
        break;
       }
      }
      if(l==1)
       break;
     }
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]<small&&num1[i][j]!=0)
        small=num1[i][j];
      }
     }
     k=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]==small)
       {
        num[k][0]=i;
        num[k][1]=j;
        num[k][2]=num1[i][j];
        k++;
        break;
       }
      }
      if(k==1)
       break;
     }
     l=3;
     i=0;
     for(j=1;j<=9;j++)
     {
      if(possibility(num[i][0],num[i][1],j)==1)
      {
       num[i][l]=j;
       l++;
      }
     }

     return k;
    }


    print()
    {
     int x,y,i,j,z;
     char str[9]="RAGHURAM";
     clrscr();
     if(m==1||m==2)
      printf("\a");
     printf("\t\t%s",str);
     printf("'s Version of SU|DO|KU Solver \n");
     if(m==1)
      printf("Press Enter To Give Input");
     if(m==2)
      printf("Press Enter To See The answer ");
     printf("\n");
     if(m==1||m==2)
      getch();
     m+=2;
     printf("\t C ");
     for(x=0;x<9;x++)
     {
      if(x/3==0)
       printf("");
      printf("%d   ",x+1);
     }
     printf("\n\t%c",201);
     for(i=0;i<35;i++)
     {
      z=205;
      if(i==11|i==23)
       z=203;
      if(i==3||i==7||i==15||i==19||i==27||i==31)
       z=209;
      printf("%c",z);
     }
     printf("%c\n   R",187);
     for(x=0;x<9;x++)
     {
      printf("\t\b%d%c",x+1,186);
      for(y=0;y<9;y++)
      {
       z=179;
       if(y==2||y==5||y==8)
        z=186;
       printf(" %c %c",(board[x][y]==0?32:48+board[x][y]),z);
      }
      if(x==8)
       break;
      if(x==2||x==5)
      {
       printf("\n\t%c",204);
       for(i=0;i<35;i++)
       {
        z=205;
        if(i==11||i==23)
         z=206;
        if(i==3||i==7||i==15||i==19||i==27||i==31)
         z=216;
        printf("%c",z);
       }
       printf("%c\n",185);
       continue;
      }
                    printf("\n\t%c",199);
      for(i=0;i<35;i++)
      {
       z=196;
       if(i==11||i==23)
        z=215;
       if(i==3||i==7||i==15||i==19||i==27||i==31)
        z=197;
       printf("%c",z);
      }
      printf("%c\n",182);
     }
     printf("\n\t%c",200);
     for(i=0;i<35;i++)
     {
      z=205;
      if(i==11||i==23)
       z=202;
      if(i==3||i==7||i==15||i==19||i==27||i==31)
       z=207;
      printf("%c",z);
     }
     printf("%c\n",188);
     return;
    }


    input()
    {
     char a;
     char z;
     int i,j,n,k,nn;
     for(;;)
     {
      nn=0;
      for(i=0;i<9;i++)
       for(j=0;j<9;j++)
        if(board[i][j]!=0)
         nn++;
      gotoxy(46,6);
      printf("ENTER ROW NUMBER(To end '0') :   \b\b");
     starti:
      z=getche();
      if(z==26)
       exit();
      if(z<48||z>57)
      {
       gotoxy(77,6);
       printf(" \a\b");
       goto starti;
      }
      if(z=='0')
      {
       if(nn>20)
        break;
       else
       {
        gotoxy(46,7);
        printf("More input Required! Press any key");
        getch();
        gotoxy(46,7);
        for(i=0;i<35;i++)
         printf(" ");
        continue;
       }
      }
      i=z-48;
     start:
      gotoxy(46,7);
      printf("ENTER COLUMN NUMBER :   \b\b");
         startj:
      z=getche();
      if(z<49||z>57)
      {
       gotoxy(68,7);
       printf(" \a\b");
       goto startj;
      }
      j=z-48;
      i=i-1;
      j=j-1;
        start1:
      gotoxy(46,8);
      printf("ENTER THE NUMBER : ");
        startn:
      z=getche();
      if(z<48||z>57)

      {
       gotoxy(65,8);
       printf(" \a\b");
       goto startn;
      }
      n=z-48;
      for(k=0;k<10000;k++)
       printf(" \b");
      if(n==board[i][j])
      {
       clrscr();
       print();
       continue;
      }
      if(possibility(i,j,n)==0 && n!=0)
      {
       gotoxy(46,9);
       printf("Number %d is already in the present",n);
       gotoxy(46,10);
       printf("in row/column/3*3 matrix .");
       gotoxy(46,11);
       printf("Enter a valid number!\a");
       getch();
       print();
       fflush(stdin);
       continue;
      }
      if(board[i][j]!=0)
      {
       gotoxy(46,9);
       printf("Position (%d,%d) already contains %d!\a",i+1,j+1,board[i][j]);
       gotoxy(46,10);
       printf("Want to replace it with %d:(y/n)\?",n);
      startx:
       a=getch();
       if(a=='y'||a=='Y')
       {
             board[i][j]=n;
             print();
             fflush(stdin);
             continue;
       }
       if(a=='n'||a=='N')
       {
        print();
        fflush(stdin);
        continue;
       }
       else
       {
        gotoxy(79,10);
        goto startx;
       }
      }

      board[i][j]=n;
      print();
     }
     return;
    }

     

     

    Tuesday, May 29, 2007 2:52 PM
  • hey it's computer who has to solve sudoku not a human.
    we'll supply a computer with a sudoku puzzle & then it has to solve it on his own.
    Tuesday, May 29, 2007 2:57 PM
  • i don't want this.
    what i want is
    see you are given a sudoku puzzle to solve it.
    in the same way, computer will be given a sudoku puzzle to solve it.
    got it.
    try this man.
    Tuesday, May 29, 2007 3:08 PM
  • sorry yaar.
    when i saw a section of your code asking the user to change the number, i thought as if you are asking the user to solve the puzzles.
    i'll check this code & surely mark your answer.
    give me some time
    Wednesday, May 30, 2007 1:32 AM

All replies

  • hmmmmmmm go to think about it.
    Sunday, May 27, 2007 2:43 AM
  • yeah every1 has to.
    think about artificial intelligence.
    Sunday, May 27, 2007 3:00 AM
  • hey everybody got puzzled or hibernating?
    i have given you a hint.
    apply AI technique.
    if you don't know what sudoku is then goto

    www.sudoku.com
    Sunday, May 27, 2007 6:43 AM
  • I have coded a program for sudoku solving.. Those of you who want the program, mail me at raghuramdcbe@gmail.com  and i'll mail you the code.. My program is in c language.. And one request, while you mail , give a few details about you so that i shall know about you guys..
    Monday, May 28, 2007 2:10 AM
  • Hi friends I made an application in .NET which is same as SUDOKU
    Monday, May 28, 2007 5:38 AM
  • post here.
    that's why i started this thread.
    Monday, May 28, 2007 6:23 AM
  • that's nice budy.
    all i want to see is LOGIC
    whichever language you use doesn't matter much
    Monday, May 28, 2007 6:25 AM
  • Code Snippet : SODOKU Solver

     

    THIS CODE WORKS IN TC ONLY !!! 

     

    #include<stdio.h>


    int board[9][9],m=1,s=1;

     

    void intro()
    {
     clrscr();
     gotoxy(30,5);
     printf("Sudoku Solver\n\n\n");
     printf("\n\n\t\tProgrammer: Raghuram Duraisamy\n\n");
     printf("\t\tE-mail Address : Raghuramdcbe@gmail.com");
     getch();
    }

     

    main()
    {
     intro();
     print();
     input();
     gotoxy(20,23);
     printf("PLEASE WAIT ");
     solver();
     if(s==1)
      printf("\n\nNo solution exists\nPress any key to exit");
     getch();
     return;
    }

     

    solver()
    {
     static int num1[9][9];
     int i,j,num[1][12]={0},n=0;
     for(i=0;i<9;i++)
      for(j=0;j<9;j++)
       num1[i][j]=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(board[i][j]!=0)
        n++;
      }
     }
     if(n==81)
     {
      m=2;
      s=2;
      print();
      getch();
      clrscr();
      printf("\n\n\n\n\n\t\t\t\tBYE!BYE!");
      getch();
      exit();
     }
     else
     {
      findNoNum(num1);
      n=findNum(num1,num);
      i=0;
      if(n>0)
      {
       for(j=3;j<3+num[i][2];j++)
       {
        board[num[i][0]][num[i][1]]=num[i][j];
        solver();
       }
       board[num[i][0]][num[i][1]]=0;
      }
      }
      return;
    }


    findNoNum(int num1[][9])
    {
     int check1;
     int i,j,k;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(board[i][j]==0)
       {
        for(k=1;k<=9;k++)
        {
         check1=possibility(i,j,k);
         if(check1==1)
         {
          num1[i][j]++;
         }
        }
       }
      }
     }
     return;
    }


    possibility(int i,int j,int k)
    {
     int l,i_s,i_e,j_s,j_e,m;
     int check1=1;
     for(l=0;l<9;l++)
     {
      if(board[i][l]==k)
      {
       check1=0;
       break;
      }
     }
     if(check1==0)
      return(check1);
     for(l=0;l<9;l++)
     {
      if(board[l][j]==k)
      {
       check1=0;
       break;
      }
     }
     if(check1==0)
      return(check1);
     switch(i/3)
     {
      case 0:
       i_s=0;
       i_e=2;
       break;
      case 1:
       i_e=5;
       i_s=3;
       break;
      case 2:
       i_s=6;
       i_e=8;
       break;
     }
     switch(j/3)
     {
      case 0:
       j_s=0;
       j_e=2;
       break;
      case 1:
       j_e=5;
       j_s=3;
       break;
      case 2:
       j_s=6;
       j_e=8;
       break;
     }
     for(l=i_s;l<=i_e;l++)
     {
      for(m=j_s;m<=j_e;m++)
      {
       if(board[l][m]==k)
       {
        check1=0;
        break;
       }
      }
      if(check1==0)
       break;
     }
     return(check1);
    }


    findNum(int num1[9][9],int num[][12])
    {
     int i,j,k=0,small,l=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]!=0)
       {
        small=num1[i][j];
        l=1;
        break;
       }
      }
      if(l==1)
       break;
     }
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]<small&&num1[i][j]!=0)
        small=num1[i][j];
      }
     }
     k=0;
     for(i=0;i<9;i++)
     {
      for(j=0;j<9;j++)
      {
       if(num1[i][j]==small)
       {
        num[k][0]=i;
        num[k][1]=j;
        num[k][2]=num1[i][j];
        k++;
        break;
       }
      }
      if(k==1)
       break;
     }
     l=3;
     i=0;
     for(j=1;j<=9;j++)
     {
      if(possibility(num[i][0],num[i][1],j)==1)
      {
       num[i][l]=j;
       l++;
      }
     }

     return k;
    }


    print()
    {
     int x,y,i,j,z;
     char str[9]="RAGHURAM";
     clrscr();
     if(m==1||m==2)
      printf("\a");
     printf("\t\t%s",str);
     printf("'s Version of SU|DO|KU Solver \n");
     if(m==1)
      printf("Press Enter To Give Input");
     if(m==2)
      printf("Press Enter To See The answer ");
     printf("\n");
     if(m==1||m==2)
      getch();
     m+=2;
     printf("\t C ");
     for(x=0;x<9;x++)
     {
      if(x/3==0)
       printf("");
      printf("%d   ",x+1);
     }
     printf("\n\t%c",201);
     for(i=0;i<35;i++)
     {
      z=205;
      if(i==11|i==23)
       z=203;
      if(i==3||i==7||i==15||i==19||i==27||i==31)
       z=209;
      printf("%c",z);
     }
     printf("%c\n   R",187);
     for(x=0;x<9;x++)
     {
      printf("\t\b%d%c",x+1,186);
      for(y=0;y<9;y++)
      {
       z=179;
       if(y==2||y==5||y==8)
        z=186;
       printf(" %c %c",(board[x][y]==0?32:48+board[x][y]),z);
      }
      if(x==8)
       break;
      if(x==2||x==5)
      {
       printf("\n\t%c",204);
       for(i=0;i<35;i++)
       {
        z=205;
        if(i==11||i==23)
         z=206;
        if(i==3||i==7||i==15||i==19||i==27||i==31)
         z=216;
        printf("%c",z);
       }
       printf("%c\n",185);
       continue;
      }
                    printf("\n\t%c",199);
      for(i=0;i<35;i++)
      {
       z=196;
       if(i==11||i==23)
        z=215;
       if(i==3||i==7||i==15||i==19||i==27||i==31)
        z=197;
       printf("%c",z);
      }
      printf("%c\n",182);
     }
     printf("\n\t%c",200);
     for(i=0;i<35;i++)
     {
      z=205;
      if(i==11||i==23)
       z=202;
      if(i==3||i==7||i==15||i==19||i==27||i==31)
       z=207;
      printf("%c",z);
     }
     printf("%c\n",188);
     return;
    }


    input()
    {
     char a;
     char z;
     int i,j,n,k,nn;
     for(;;)
     {
      nn=0;
      for(i=0;i<9;i++)
       for(j=0;j<9;j++)
        if(board[i][j]!=0)
         nn++;
      gotoxy(46,6);
      printf("ENTER ROW NUMBER(To end '0') :   \b\b");
     starti:
      z=getche();
      if(z==26)
       exit();
      if(z<48||z>57)
      {
       gotoxy(77,6);
       printf(" \a\b");
       goto starti;
      }
      if(z=='0')
      {
       if(nn>20)
        break;
       else
       {
        gotoxy(46,7);
        printf("More input Required! Press any key");
        getch();
        gotoxy(46,7);
        for(i=0;i<35;i++)
         printf(" ");
        continue;
       }
      }
      i=z-48;
     start:
      gotoxy(46,7);
      printf("ENTER COLUMN NUMBER :   \b\b");
         startj:
      z=getche();
      if(z<49||z>57)
      {
       gotoxy(68,7);
       printf(" \a\b");
       goto startj;
      }
      j=z-48;
      i=i-1;
      j=j-1;
        start1:
      gotoxy(46,8);
      printf("ENTER THE NUMBER : ");
        startn:
      z=getche();
      if(z<48||z>57)

      {
       gotoxy(65,8);
       printf(" \a\b");
       goto startn;
      }
      n=z-48;
      for(k=0;k<10000;k++)
       printf(" \b");
      if(n==board[i][j])
      {
       clrscr();
       print();
       continue;
      }
      if(possibility(i,j,n)==0 && n!=0)
      {
       gotoxy(46,9);
       printf("Number %d is already in the present",n);
       gotoxy(46,10);
       printf("in row/column/3*3 matrix .");
       gotoxy(46,11);
       printf("Enter a valid number!\a");
       getch();
       print();
       fflush(stdin);
       continue;
      }
      if(board[i][j]!=0)
      {
       gotoxy(46,9);
       printf("Position (%d,%d) already contains %d!\a",i+1,j+1,board[i][j]);
       gotoxy(46,10);
       printf("Want to replace it with %d:(y/n)\?",n);
      startx:
       a=getch();
       if(a=='y'||a=='Y')
       {
             board[i][j]=n;
             print();
             fflush(stdin);
             continue;
       }
       if(a=='n'||a=='N')
       {
        print();
        fflush(stdin);
        continue;
       }
       else
       {
        gotoxy(79,10);
        goto startx;
       }
      }

      board[i][j]=n;
      print();
     }
     return;
    }

     

     

    Tuesday, May 29, 2007 2:52 PM
  • Guys, the input and the printing part is what that makes my code much longer...

    The actual solving is done by the following functions

    solver() - the main part of solving , this is where the numbers are inserted and deleted in the array

    findNoNum() - finds the number of possible elements that can fit into a box

    findNum() - finds the possible elements that can fit in a box

    possiblity() - checks whether a element can fit in a box

     

     

    *'fit in a box' - by this , i mean whether a element(number 1 to 9) can be placed in a box according to the rules of Sudoku...

     

    Tuesday, May 29, 2007 2:57 PM
  • hey it's computer who has to solve sudoku not a human.
    we'll supply a computer with a sudoku puzzle & then it has to solve it on his own.
    Tuesday, May 29, 2007 2:57 PM
  • HOW TO ENTER THE INPUT IN THE SUDOKU SOLVER

     

    Press any key twice , to pass the Introduction screens


    NOTE:
    1)The rows as well as the columns are numbered from 1

    to 9
    2)R ->Rows , C->Columns

     

    Now you can start the input.


    1)To input a number in a box, you need to type the row

    number first, and then the column number , followed by

    the number
    NOTE: You need not press enter after entering the

    numbers

     

    2)If you enter a number erroroneously in a box, don't

    worry.Type the row and column number again , and
    a)enter 0 if the box is to contain no number or
    b)enter the appropriate number if some other number has

    to be entered
    and press 'y' for confirmation of the replacement

    If you recognise that you had entered a wrong row

    number or column number , just press 0 when the input

    asks for "NUMBER"


     3) When you have completed entering the input press 0

    at row number


     

    Tuesday, May 29, 2007 2:59 PM
  • Guys, try my sudoku solver program and please give your valuable  suggestions..
    Tuesday, May 29, 2007 3:02 PM
  • @Sunil - ya , my program gets input from the user, then computes the result and displays it..
    Tuesday, May 29, 2007 3:03 PM
  • i don't want this.
    what i want is
    see you are given a sudoku puzzle to solve it.
    in the same way, computer will be given a sudoku puzzle to solve it.
    got it.
    try this man.
    Tuesday, May 29, 2007 3:08 PM
  • @ Sunil - Hey , how do you give the sudoku puzzle to the computer? You need to feed the values to enter the puzzle..

    Thats what i get as input..

    Then , my program computes the solution for the sudoku puzzle and dispalys it..

     

    If you are not still satisfied with my answer, first try my program in TC (enter a sudoku puzzle as per the Input instructions) and observe...

     

    Or you can find me raghuramdcbe@gmail.com in gtalk.. We will better discuss it there if you have further doubts...

    Tuesday, May 29, 2007 3:53 PM
  • sorry yaar.
    when i saw a section of your code asking the user to change the number, i thought as if you are asking the user to solve the puzzles.
    i'll check this code & surely mark your answer.
    give me some time
    Wednesday, May 30, 2007 1:32 AM
  • @Sunil -  I have included some code to avoid entry of wrong puzzles .. You should have got confused by that...

    Take your own time man.. I would be glad to hear your suggestions..
    Wednesday, May 30, 2007 4:35 AM
  • great program man...
    Wednesday, May 30, 2007 5:30 PM
  • @Anoop- Thanks for your encouragement.. I would feel great if you would come with any suggestions...
    Thursday, May 31, 2007 1:14 AM
  • Great prog Raghuram, will definitely look over it in detail and give you suggestions if any.
    Thursday, May 31, 2007 8:16 AM