Showing posts with label sudoku. Show all posts
Showing posts with label sudoku. Show all posts
C++ : Sudoku Solver
This C++ Program demonstrates the Sudoku Problem using Backtracking.
Here is source code of the C++ Program to solve the Sudoku Problem
using BackTracking. The C++ program is successfully compiled and run on a
Linux system. The program output is also shown below. Code:
/*C++ Program to Solve Sudoku Problem using BackTracking */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define UNASSIGNED 0
#define N 9
bool FindUnassignedLocation(int grid[N][N], int &row, int &col);
bool isSafe(int grid[N][N], int row, int col, int num);
/* assign values to all unassigned locations for Sudoku solution
*/
bool SolveSudoku(int grid[N][N])
{
int row, col;
if (!FindUnassignedLocation(grid, row, col))
return true;
for (int num = 1; num <= 9; num++)
{
if (isSafe(grid, row, col, num))
{
grid[row][col] = num;
if (SolveSudoku(grid))
return true;
grid[row][col] = UNASSIGNED;
}
}
return false;
}
/* Searches the grid to find an entry that is still unassigned. */
bool FindUnassignedLocation(int grid[N][N], int &row, int &col)
{
for (row = 0; row < N; row++)
for (col = 0; col < N; col++)
if (grid[row][col] == UNASSIGNED)
return true;
return false;
}
/* Returns whether any assigned entry n the specified row matches
the given number. */
bool UsedInRow(int grid[N][N], int row, int num)
{
for (int col = 0; col < N; col++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry in the specified column matches
the given number. */
bool UsedInCol(int grid[N][N], int col, int num)
{
for (int row = 0; row < N; row++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry within the specified 3x3 box matches
the given number. */
bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row+boxStartRow][col+boxStartCol] == num)
return true;
return false;
}
/* Returns whether it will be legal to assign num to the given row,col location.
*/
bool isSafe(int grid[N][N], int row, int col, int num)
{
return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) &&
!UsedInBox(grid, row - row % 3 , col - col % 3, num);
}
/* print grid */
void printGrid(int grid[N][N])
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
cout<<grid[row][col]<<" ";
cout<<endl;
}
}
/* Main */
int main()
{
int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
if (SolveSudoku(grid) == true)
printGrid(grid);
else
cout<<"No solution exists"<<endl;
return 0;
}
Output:
$ g++ sudoku.cpp
$ a.out
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9
------------------
(program exited with code: 1)
Press return to continue
C: Sudoku Solver
Sudoku Game in 'C'
![]() |
Figure: Sudoku |
Here i have developed a program in C to solve Sudoku for 2x2 to 9x9
matrix! the code will ask for no of rows and columns at first, it means
you have to add 2 by 2,or 3x3,or ...9x9, now give the inputs one by
one and after entering each i/p press enter, after finishing giving
i/ps,it will show the sudoku matrix, and you can verify, whether your
sodoku is correct or incorrect. And if incorrect, which column or row has
the wrong i/p, try out this new code. An example is shown with 3X3
matrix, you can try out with diff matrix even. Hoping to have Good
Response.
Code:
//**source code in C**//#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,n,s;
int k,l;
int value[9][9];
int total_row[9];
int total_col[9];
printf("'enter the inputs of a soduko,and check whether its correct or not'\n");
printf("\n input no of 'rows x column' \n");
scanf("\n%d",&k);
scanf("\t%d",&l);
printf("values\n");
for(i=0;i<k;i++)
{
total_row[i]=0;
for(j=0;j<l;j++)
{
scanf("%d",&value[i][j]);
total_row[i]=total_row[i]+value[i][j];
}
}
for(j=0;j<l;j++)
{
total_col[j]=0;
for(i=0;i<k;i++)
{
total_col[j]=total_col[j]+value[i][j];
}
}
for(i=0;i<k;i++)
{
printf("\n");
for(j=0;j<l;j++)
{
printf(" \t %d ",value[i][j]);
}
}
s=((k*(k+1))/2);
for(n=0;n<k;n++)
{
if((total_col[n])!=s)
printf("\n incorrect i/p's col_%d ",n+1 );
else if((total_row[n])!=s)
printf(" incorrect i/p's row_%d ",n+1);
else if(total_row[n] && s && total_col[n])
printf("\n correct i/p for both %d row and col ",n+1);
else if(total_row[n] && s )
printf("\n correct i/p for %d row ",n+1);
else if(s && total_col[n])
printf("\n correct i/p for %d col ",n+1);
else
{
}
}
//fflush(stdin);
getch();
getch();
getch();
getch();
getch();
getch();
getch();
getch();
}
Output:
Subscribe to:
Posts (Atom)
C Programming Online Test 1
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...

-
Character I/O: Visual Basic provides some excellent ways for input and output operations. Input can be taken using TextBox, InputBox, ...
-
Design a form contains a sorted list alphabetically such that the user can add the item from text to the list after click on command button...
-
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...