-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathN_QueenPrb.cpp
More file actions
115 lines (97 loc) · 1.6 KB
/
N_QueenPrb.cpp
File metadata and controls
115 lines (97 loc) · 1.6 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//n-queen problem it took a hell lot of time and i am not able to understand why am i not that happy !
#include <iostream>
using namespace std;
void set_the_map(char board[1000][1000],int n,char ch)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
board[i][j]='x';
}
}
}
void get_the_map(char board[1000][1000],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<board[i][j]<<" ";
}
cout<<endl;
}
}
bool canPlace(char board[1000][1000],int row,int col,int n)
{
for(int i=0;i<n;i++)
{
if(board[i][col]=='Q'|| board[row][i]=='Q')
{
return false;
}
int RowInc[]={-1,-1,1,1};
int colInc[]={-1,1,1,-1};
for(int i=0;i<4;i++)
{
int curRowInc = RowInc[i];
int curColInc = colInc[i];
int starti=row;
int startj=col;
while(starti>=0&&startj<n &&starti<n&&startj>=0 )
{
if(board[starti][startj]=='Q')
{
return false;
}
starti+=curRowInc;
startj+=curColInc;
}
}
}
return true;
}
bool PlaceQueen(char board[1000][1000],int crow,int n)
{
if(crow==n)
{
return true;
}
for(int c=0;c<n;c++)
{
if(canPlace(board,crow,c,n)==true)
{
board[crow][c]='Q';
if(PlaceQueen(board,crow+1,n))
{
return true;
}
board[crow][c]='x';
}
}
return false;
}
int main()
{
int n;
cin>>n;
char board[1000][1000];
set_the_map(board,n,'R');
bool ispossible = PlaceQueen(board,0,n);
if( ispossible ==true)
{
get_the_map(board,n);
}
else
{
cout<<"\n No can do this will baby doll !";
}
}
node * MergeSort(node * A,int beg,int end)
{
while(beg<=end)
{
int mid= (beg+end)/2;
MergeSort(node)
}
}