Tuesday 4 September 2012

Initialise an array in spiral form (inside out).


/*

Initialise the center element of the matrix with 1 and then assign all other cells in spiral order.
21 22 23 24 25
26  7   8   9  10
19  6   1   2  11
18  5   4   3  12
17 16 15 14 13

*/

// this will work only for odd number of rows and columns square matrix as in even rows and columns square //matrix there is no center element.


#include<iostream.h>
#include<conio.h>
const int rowcol=5;
int matrix[rowcol][rowcol];
void spiralinvert(int row,int col)
{
     int count=2;
     int cstart=col/2,cend=cstart+1;
     int rstart=row/2,rend=rstart+1;
     int n=row*col;
     matrix[cstart][rstart]=1;
     cout<<"spiral starts"<<endl;
     while(1)
     {
                   
                   for(int i=cstart+1;i<=cend;i++,count++)
                           matrix[rstart][i]=count;
                   cstart--;
                   if(count>=n)
                               break;

                   for(int j=rstart+1;j<=rend;j++,count++)
                                   matrix[j][cend]=count;
                   rstart--;
                   if(count>=n)
                               break;

                   for(int k=cend-1;k>=cstart;k--,count++)
                           matrix[rend][k]=count;
                   cend++;
                   if(count>=n)
                      break;

                   for(int l=rend-1;l>=rstart;l--,count++)
                           matrix[l][cstart]=count;
                   rend++;
                   if(count>=n)
                            break;
     }
     cout<<"spiral ends"<<endl;
}

void display(int row,int col)
{
     for(int i=0;i<row;i++)
     {
             cout<<"\n";
             for(int j=0;j<col;j++)
                     cout<<"\t"<<matrix[i][j];
     }
}

int main()
{
    spiralinvert(rowcol,rowcol);
    display(rowcol,rowcol);
    getch();
    return 0;
}

Print 2D array in spiral form.

/*
if array is 
1    2   3   4   5
6    7   8   9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

output:- 1 2 3 4 5 10 15 20 25 24 23 22 21 16 11 6 7 8 9 14 19 18 17 12 13

*/

#include<iostream.h>
#include<conio.h>

int matrix[5][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};

void spiralprint(int row,int col)
{
     int cstart=0,cend=col-1,rstart=0,rend=row-1,n=col*row;
     int count=0;
     while(count<n)
     {
                   for(int i=cstart;i<=cend;i++,count++)
                           cout<<"  "<<matrix[rstart][i];
                   rstart++;
                   for(int j=rstart;j<=rend;j++,count++)
                           cout<<"  "<<matrix[j][cend];
                   cend--;
                   for(int k=cend;k>=cstart;k--,count++)
                           cout<<"  "<<matrix[rend][k];
                   rend--;
                   for(int l=rend;l>=rstart;l--,count++)
                           cout<<"  "<<matrix[l][cstart];
                   cstart++;
     }
                   
}

int main()
{
    spiralprint(5,5);
    getch();
    return 0;
}

Monday 3 September 2012

SUDOKU SOLVER

/*

  • move from left to right and top to down.
  • In the empty cell try 1 to 9 and chose the first one that fit i.e chosen no. should not conflict with any entry in the same row and column.
  • In a 9x9 sudoku there are 9 small(3x3) matrix, the chosen number should be unique in this smaller(3x3) matrix.
  • Now move on to next cell and repeat the same procedure.
  • If no number fits in this cell then backtrack(return false) to the previous made decision and change it to next higher value that fit.
  • If no value fits in this cell backtrack to next higher level.
*/

#include<iostream.h>
#include<conio.h>
int sudoku[9][9]={{0,0,4,3,0,2,1,0,0},{0,6,0,0,0,0,0,2,0},{9,0,0,0,0,0,0,0,7},
                  {0,0,1,0,3,0,2,0,0},{2,0,0,9,0,6,0,0,1},{0,0,9,0,5,0,4,0,0},
                  {7,0,0,0,0,0,0,0,3},{0,1,0,0,0,0,0,4,0},{0,0,8,5,0,3,9,0,0}};
                 
bool findunassignedcell(int &r, int &c)
{
     for(int i=0;i<9;i++)
     {
             for(int j=0;j<9;j++)
             {
                     if(sudoku[i][j]==0)
                     {
                                        r=i;
                                        c=j;
                                        return true;
                     }
             }
     }
     return false;
}

void displaysudoku()
{
     cout<<"\n\n";
     for(int i=0;i<9;i++)
     {
              cout<<"\n";
              for(int j=0;j<9;j++)
                      cout<<"  "<<sudoku[i][j];
     }
}

bool noconflict(int r,int c,int n)
{
   bool flag=true;
   for(int i=0;i<9;i++)
   {
           if(sudoku[i][c]==n && i!=r)
                              flag=false;
   }
   for(int j=0;j<9;j++)
   {
           if(sudoku[r][j]==n && j!=c)
                              flag=false;
   }
  
   int rstart=r-r%3,cstart=c-c%3;
   int rend=rstart+2,cend=cstart+2;
  
   for(int p=rstart;p<=rend;p++)
   {
           for(int q=cstart;q<=cend;q++)
           {
                   if(sudoku[p][q]==n && (p!=r && q!=c))
                                      flag=false;
           }
   }
   return flag;
}
            

bool solvesudoku()
{
     int row,col;
     if(!findunassignedcell(row,col))
     {
                                     displaysudoku();
                                     return true;
     }
     for(int num=1;num<=9;num++)
     {
             if(noconflict(row,col,num))
             {
                                      sudoku[row][col]=num;//assign
             }
             else
                 continue;
             if(solvesudoku())
                              return true;
            
              sudoku[row][col]=0;  //unassign
     }
     return false;
}

int main()
{
    cout<<"INTITIAL \n";
    displaysudoku();
    cout<<"\n\nSOLVED \n\n";
    solvesudoku();
    getch();
    return 0;
}