Skip to content

Commit 5d6ef93

Browse files
committed
Find all the solutions of NQueen
1 parent a9b1f89 commit 5d6ef93

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

Backtracking/NQueen.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
const displayBoard = (board) => {
2+
console.log("\n");
3+
for (const row of board) {
4+
console.log(...row)
5+
}
6+
}
7+
18
class NQueen {
29
constructor (size) {
310
this.board = new Array(size).fill('.').map(() => new Array(size).fill('.'))
411
this.size = size
12+
this.solutions = []
513
}
614

715
isValid ([row, col]) {
@@ -25,41 +33,43 @@ class NQueen {
2533
return true
2634
}
2735

36+
placeQueen(row, col){
37+
this.board[row][col] = 'Q'
38+
}
39+
40+
removeQueen(row, col){
41+
this.board[row][col] = '.';
42+
}
43+
2844
solve (col = 0) {
29-
// function to solve the board
30-
if (col >= this.size) { return true }
45+
if (col >= this.size) {
46+
this.solutions.push(JSON.parse(JSON.stringify(this.board)));
47+
return true
48+
}
3149

3250
for (let i = 0; i < this.size; i++) {
3351
if (this.isValid([i, col])) {
34-
this.board[i][col] = 'Q'
35-
36-
if (this.solve(col + 1)) { return true }
37-
38-
// backtracking
39-
this.board[i][col] = '.'
52+
this.placeQueen(i, col)
53+
this.solve(col + 1)
54+
this.removeQueen(i, col)
4055
}
4156
}
4257

4358
return false
4459
}
45-
46-
printBoard () {
47-
// utility function to display the board
48-
for (const row of this.board) {
49-
console.log(...row)
50-
}
51-
}
5260
}
5361

5462
function main () {
55-
const board = new NQueen(8)
56-
57-
board.printBoard()
58-
console.log('\n')
59-
60-
board.solve()
61-
62-
board.printBoard()
63+
const nQueen = new NQueen(4)
64+
displayBoard(nQueen.board)
65+
nQueen.solve()
66+
67+
console.log("Number of solutions:", nQueen.solutions.length);
68+
nQueen.solutions.forEach((solution)=> {
69+
displayBoard(solution)
70+
});
6371
}
6472

65-
main()
73+
// main()
74+
75+
export { NQueen };

Backtracking/test/NQueen.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {NQueen} from '../NQueen';
2+
3+
describe('NQueen', () => {
4+
5+
it('returns 2 solutions for 4x4 size board', ()=> {
6+
const nQueen = new NQueen(4);
7+
const result = nQueen.solve();
8+
expect(nQueen.solutions.length).toEqual(2);
9+
})
10+
11+
it('returns 92 solutions for 8x8 size board', ()=> {
12+
const nQueen = new NQueen(8);
13+
const result = nQueen.solve();
14+
expect(nQueen.solutions.length).toEqual(92);
15+
})
16+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy