-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuSolver.cpp
64 lines (59 loc) · 970 Bytes
/
SudokuSolver.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
bool canbeplaced(int arr[][9], int n, int num, int i, int j)
{
for(int l=0; l<n; l++)
{
if((arr[i][l]==num)||(arr[l][j]==num))
return false;
}
int x = (i/3)*3;
int y = (j/3)*3;
for(int a=x; a<x+3; a++)
for(int b=y; b<y+3; b++)
{
if(arr[a][b]==num)
return false;
}
return true;
}
void sudoku(int arr[][9], int n, int i,int j)
{
if(i==n)
{
for(int u=0; u<n; u++)
{
for(int q=0; q<n; q++)
cout<<arr[u][q]<<" ";
cout<<endl;
}
return;
}
if(j==n)
sudoku(arr,n,i+1,0);
else if(arr[i][j]!=0)
sudoku(arr,n,i,j+1);
else
{
for(int num=1; num<=9; num++)
{
if(canbeplaced(arr,n,num,i,j))
{
arr[i][j] = num;
sudoku(arr,n,i,j+1);
arr[i][j] = 0;
}
}
}
}
int main()
{
int n;
cin>>n;
int arr[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
cin>>arr[i][j];
cout<<endl;
sudoku(arr,n,0,0);
}