Skip to content

Commit 602aa6e

Browse files
authored
Create nqueens with obstacles
1 parent acdf00a commit 602aa6e

File tree

1 file changed

+294
-0
lines changed

1 file changed

+294
-0
lines changed

nqueens/c/nqueenswithobstacles.cpp

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<cstdio>
4+
#include<queue>
5+
#include<stack>
6+
#include<cstring>
7+
#include<cmath>
8+
#include<ctime>
9+
#include<cstdlib>
10+
11+
using namespace std;
12+
13+
#define toDigit(c) (c-'0')
14+
15+
struct node {
16+
vector< vector<int> > board;
17+
long int rowcol;
18+
int p;
19+
};
20+
21+
void printBoard(vector< vector<int> > board){
22+
int n = board.size();
23+
printf("OK\n");
24+
for(int i=0;i<n;i++){
25+
for(int j=0; j<n; j++){
26+
printf("%d", board[i][j]);
27+
}
28+
printf("\n");
29+
}
30+
}
31+
32+
bool isSafe(vector <vector<int> > board, int row, int col){
33+
int i, j, n = board.size();
34+
35+
for (i = col-1; i >= 0; i--) //left
36+
if (board[row][i] == 1)
37+
return false;
38+
else if(board[row][i] == 2)
39+
break;
40+
41+
for (i = row-1; i >= 0; i--) //up
42+
if (board[i][col] == 1)
43+
return false;
44+
else if(board[i][col] == 2)
45+
break;
46+
47+
for (i=row, j=col; i>=0 && j>=0; i--, j--) // up left
48+
if (board[i][j] == 1)
49+
return false;
50+
else if(board[i][j] == 2)
51+
break;
52+
53+
for (i=row, j=col; j>=0 && i>=0; i--, j++) // up right
54+
if (board[i][j] == 1)
55+
return false;
56+
else if(board[i][j] == 2)
57+
break;
58+
59+
return true;
60+
}
61+
62+
int findCost (vector<vector<int> > board, vector<long int> queens){
63+
64+
int cost = 0, i, j, n = board.size(), p = queens.size(), row, col;
65+
long int rowcol;
66+
67+
for(i=0; i<p;i++){
68+
rowcol = queens[i];
69+
row = rowcol/n;
70+
col = rowcol%n;
71+
72+
for (i = col-1; i >= 0; i--) //left
73+
if (board[row][i] == 1){
74+
cost++;
75+
break;
76+
}
77+
else if(board[row][i] == 2)
78+
break;
79+
80+
for (i = row-1; i >= 0; i--) //up
81+
if (board[i][col] == 1){
82+
cost++;
83+
break;
84+
}
85+
else if(board[i][col] == 2)
86+
break;
87+
88+
for (i=row, j=col; i>=0 && j>=0; i--, j--) // up left
89+
if (board[i][j] == 1){
90+
cost++;
91+
break;
92+
}
93+
else if(board[i][j] == 2)
94+
break;
95+
96+
for (i=row, j=col; j>=0 && i>=0; i--, j++) // up right
97+
if (board[i][j] == 1){
98+
cost++;
99+
break;
100+
}
101+
else if(board[i][j] == 2)
102+
break;
103+
}
104+
105+
return cost;
106+
107+
}
108+
109+
bool solveDFS(vector <vector<int> > board, long int rowcol, int p){
110+
int n = board.size();
111+
int n2 = n*n;
112+
113+
if (p == 0){
114+
printBoard(board);
115+
return true;
116+
}
117+
118+
for(long int i = rowcol; i<n2; i++){
119+
120+
int row = i/n;
121+
int col = i%n;
122+
123+
if( board[row][col] != 2 && isSafe(board, row, col) ){
124+
board[row][col] = 1;
125+
126+
if( solveDFS(board, i+1, p-1) )
127+
return true;
128+
}
129+
130+
if( board[row][col] != 2) board[row][col] = 0;
131+
}
132+
133+
return false;
134+
}
135+
136+
bool solveBFS(queue<node> container) {
137+
138+
// printBoard(container.top().board);
139+
140+
int n = container.front().board.size();
141+
long int n2 = n*n;
142+
int row, col, i;
143+
144+
while(container.size()){
145+
146+
node current = container.front();
147+
container.pop();
148+
149+
if(current.p == 0) {
150+
printBoard(current.board);
151+
return true;
152+
}
153+
154+
for(i = current.rowcol; i<n2; i++){
155+
156+
row = i/n;
157+
col = i%n;
158+
if( current.board[row][col] != 2 && isSafe(current.board, row, col) ){
159+
160+
current.board[row][col] = 1;
161+
current.rowcol = i+1;
162+
current.p -= 1;
163+
container.push(current);
164+
165+
if(current.p == 0) {
166+
printBoard(current.board);
167+
return true;
168+
}
169+
170+
//original
171+
current.board[row][col] = 0;
172+
current.rowcol = i;
173+
current.p += 1;
174+
175+
}
176+
177+
}
178+
179+
}
180+
181+
return false;
182+
183+
}
184+
185+
bool solveSA(vector<vector<int> > board, int p){
186+
int start_s=clock();
187+
int n = board.size();
188+
long int n2 = n*n, oldPlace, placeQueen;
189+
int p2 = p, i, j;
190+
int row, col, cost, newCost, pickQueen;
191+
vector<long int> queens;
192+
double T = 10000, c, coolingRate = 0.003, prob;
193+
194+
// random init
195+
while(p2){
196+
placeQueen = rand()%n2;
197+
row = placeQueen/n;
198+
col = placeQueen%n;
199+
if(board[row][col] == 0){
200+
board[row][col] = 1;
201+
p2--;
202+
queens.push_back(placeQueen);
203+
}
204+
}
205+
206+
newCost = findCost(board, queens);
207+
208+
while(T > 1 && (((clock()-start_s)/double(CLOCKS_PER_SEC)*1000) < 280000)) { //200000 milliseconds
209+
cost = newCost;
210+
pickQueen = rand() % p;
211+
oldPlace = queens[pickQueen];
212+
row = oldPlace/n;
213+
col = oldPlace%n;
214+
board[row][col] = 0;
215+
216+
placeQueen = rand() % n2;
217+
row = placeQueen/n;
218+
col = placeQueen%n;
219+
if(board[row][col] != 0)
220+
continue;
221+
222+
queens[pickQueen] = placeQueen;
223+
newCost = findCost(board, queens);
224+
if (newCost == 0){
225+
printBoard(board);
226+
return true;
227+
}
228+
229+
if (newCost < cost)
230+
continue;
231+
232+
c = exp(((double)cost-newCost)/T);
233+
p = (rand()%100)/100;
234+
if(c < p){
235+
236+
board[row][col] = 0;
237+
queens[pickQueen] = oldPlace;
238+
row = oldPlace/n;
239+
col = oldPlace%n;
240+
board[row][col] = 1;
241+
}
242+
243+
// cool system
244+
T *= 1-coolingRate;
245+
}
246+
247+
return false;
248+
249+
}
250+
251+
int main(){
252+
253+
freopen("input.txt", "r", stdin);
254+
freopen("output.txt", "w", stdout);
255+
256+
node board;
257+
board.rowcol = 0;
258+
int n, i, j, p;
259+
char cip, mode[5];
260+
vector<int> row;
261+
queue<node> containerBFS;
262+
stack<node> containerDFS;
263+
264+
scanf("%s", mode);
265+
scanf("%d %d ", &n, &p);
266+
board.p = p;
267+
for(i=0;i<n;i++){
268+
for(j=0;j<n;j++){
269+
scanf("%c", &cip);
270+
row.push_back(toDigit(cip));
271+
}
272+
getchar();
273+
board.board.push_back(row);
274+
row.clear();
275+
}
276+
277+
if(strcmp(mode, "DFS")){
278+
if(solveDFS(board.board, 0, p) == false)
279+
printf("FAIL\n");
280+
}
281+
282+
else if(strcmp(mode, "BFS")){
283+
containerBFS.push(board);
284+
if(solveBFS(containerBFS) == false)
285+
printf("FAIL\n");
286+
}
287+
288+
else {
289+
if(solveSA(board.board, p) == false)
290+
printf("Fail\n");
291+
}
292+
293+
return 0;
294+
}

0 commit comments

Comments
 (0)