Skip to content

Commit 2660ca5

Browse files
committed
Added n-queen and sudoku code
1 parent 633aa8a commit 2660ca5

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

Backtracking/Sudoku.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include<iostream>
2+
using namespace std;
3+
int n=9;
4+
void printsudoku(int mat[][9])
5+
{
6+
int i,j;
7+
for(i=0;i<n;i++)
8+
{
9+
for(j=0;j<n;j++)
10+
{
11+
cout<<mat[i][j]<<" ";
12+
if((j+1)%3==0)
13+
{
14+
cout<<'\t';
15+
}
16+
}
17+
if((i+1)%3==0)
18+
{
19+
cout<<endl;
20+
}
21+
cout<<endl;
22+
}
23+
}
24+
bool possible(int mat[][9],int i,int j,int no)
25+
{
26+
int x,y;
27+
for(x=0;x<n;x++)
28+
{
29+
if(mat[x][j]==no || mat[i][x]==no)
30+
{
31+
return false;
32+
}
33+
}
34+
int m=(i/3)*3;
35+
int p=(j/3)*3;
36+
for(x=m;x<m+3;x++)
37+
{
38+
for(y=p;y<p+3;y++)
39+
{
40+
if(mat[x][y]==no)
41+
{
42+
return false;
43+
}
44+
}
45+
}
46+
return true;
47+
}
48+
bool solvesudoku(int mat[][9],int i,int j)
49+
{
50+
if(i==9)
51+
{
52+
printsudoku(mat);
53+
return true;
54+
}
55+
56+
if (j==9)
57+
{
58+
return solvesudoku(mat,i+1,0);
59+
}
60+
61+
if(mat[i][j]!=0)
62+
{
63+
return solvesudoku(mat,i,j+1);
64+
}
65+
66+
for(int no=1;no<=9;no++)
67+
{
68+
if(possible(mat,i,j,no))
69+
{
70+
mat[i][j]=no;
71+
bool aagekisolvehui=solvesudoku(mat,i,j+1);
72+
if(aagekisolvehui)
73+
{
74+
return true;
75+
}
76+
}
77+
}
78+
mat[i][j]=0;
79+
return false;
80+
}
81+
int main()
82+
{
83+
int mat[][9]={
84+
{5,3,0,0,7,0,0,0,0},
85+
{6,0,0,1,9,5,0,0,0},
86+
{0,9,8,0,0,0,0,6,0},
87+
{8,0,0,0,6,0,0,0,3},
88+
{4,0,0,8,0,3,0,0,1},
89+
{7,0,0,0,2,0,0,0,6},
90+
{0,6,0,0,0,0,2,8,0},
91+
{0,0,0,4,1,9,0,0,5},
92+
{0,0,0,0,8,0,0,7,9}
93+
};
94+
95+
printsudoku(mat);
96+
solvesudoku(mat,0,0);
97+
}

Backtracking/nQueen.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include<iostream>
2+
using namespace std;
3+
bool safequeen(int board[][10],int i,int j,int n)
4+
{
5+
for(int row=0;row<i;row++)
6+
{
7+
if(board[row][j]==1)
8+
{
9+
return false;
10+
}
11+
}
12+
13+
int x=i;
14+
int y=j;
15+
while(x>=0 && y>=0)
16+
{
17+
if(board[x][y]==1)
18+
{
19+
return false;
20+
}
21+
x--;
22+
y--;
23+
}
24+
25+
x=i;
26+
y=j;
27+
while(x>=0 && y<n)
28+
{
29+
if(board[x][y]==1)
30+
{
31+
return false;
32+
}
33+
x--;
34+
y++;
35+
}
36+
37+
return true;
38+
}
39+
bool nqueen(int board[][10],int i,int n)
40+
{
41+
if(i==n)
42+
{
43+
for(int i=0;i<n;i++)
44+
{
45+
for(int j=0;j<n;j++)
46+
{
47+
if(board[i][j]==1)
48+
{
49+
cout<<"Q";
50+
}
51+
else
52+
{
53+
cout<<"_";
54+
}
55+
}
56+
cout<<endl;
57+
}
58+
cout<<endl;
59+
return false;
60+
}
61+
for(int j=0;j<n;j++)
62+
{
63+
if(safequeen(board,i,j,n))
64+
{
65+
board[i][j]=1;
66+
if(nqueen(board,i+1,n))
67+
{
68+
return true;
69+
}
70+
board[i][j]=0;
71+
}
72+
}
73+
return false;
74+
}
75+
int main()
76+
{
77+
int n;
78+
cin>>n;
79+
int board[10][10]={0};
80+
nqueen(board,0,n);
81+
return 0;
82+
}

0 commit comments

Comments
 (0)