• No results found

The N-Queens Problem

N/A
N/A
Protected

Academic year: 2020

Share "The N-Queens Problem"

Copied!
29
0
0

Loading.... (view fulltext now)

Full text

(1)

The N-Queens Problem

Suppose you have 8 Suppose you have 8 chess queens...

chess queens...

...and a chess board...and a chess board

(2)

The N-Queens Problem

Can the queens be

placed on the board so that no two queens are attacking each other

??

(3)

The N-Queens Problem

Two queens are not Two queens are not allowed in the same allowed in the same row...

row...

(4)

The N-Queens Problem

Two queens are not Two queens are not allowed in the same allowed in the same row, or in the same row, or in the same column...

column...

(5)

The N-Queens Problem

Two queens are not Two queens are not allowed in the same allowed in the same row, or in the same row, or in the same column, or along the column, or along the same diagonal.

same diagonal.

(6)

The N-Queens Problem

The number of queens, The number of queens, and the size of the board and the size of the board can vary.

can vary.

N Queens

N rows

N columns

(7)

The N-Queens Problem

We will write a program We will write a program which tries to find a way which tries to find a way to place N queens on an to place N queens on an N N xx N chess board. N chess board.

(8)

How the program works

The program The program uses a stack to uses a stack to keep track of keep track of where each where each

queen is placed.

queen is placed.

(9)

How the program works

Each time the Each time the program decides program decides to place a queen to place a queen on the board, on the board, the position of the position of the new queen is the new queen is stored in a

stored in a

record which is record which is placed in the placed in the stack.

stack.

ROW 1, COL 1

(10)

How the program works

We also have an We also have an integer variable integer variable to keep track of to keep track of how many rows how many rows have been filled have been filled so far.

so far.

ROW 1, COL 1

1 filled

(11)

How the program works

Each time we try Each time we try to place a new to place a new queen in the next queen in the next row, we start by row, we start by placing the

placing the

queen in the first queen in the first column...

column... ROW 1, COL 1

1 filled

ROW 2, COL 1

(12)

How the program works

...if there is a ...if there is a conflict with conflict with another queen, another queen, then we shift the then we shift the new queen to the new queen to the next column.

next column.

ROW 1, COL 1

1 filled

ROW 2, COL 2

(13)

How the program works

If another If another

conflict occurs, conflict occurs, the queen is

the queen is

shifted rightward shifted rightward again.

again.

ROW 1, COL 1

1 filled

ROW 2, COL 3

(14)

How the program works

When there are When there are no conflicts, we no conflicts, we stop and add one stop and add one to the value of to the value of

filled.

filled.

ROW 1, COL 1

2 filled

ROW 2, COL 3

(15)

How the program works

Let's look at the Let's look at the third row. The third row. The first position we first position we try has a

try has a conflict...

conflict...

ROW 1, COL 1

2 filled

ROW 2, COL 3 ROW 3, COL 1

(16)

How the program works

...so we shift to ...so we shift to column 2. But column 2. But another conflict another conflict arises...

arises...

ROW 1, COL 1

2 filled

ROW 2, COL 3 ROW 3, COL 2

(17)

How the program works

...and we shift to ...and we shift to the third column.

the third column.

Yet another Yet another

conflict arises...

conflict arises...

ROW 1, COL 1

2 filled

ROW 2, COL 3 ROW 3, COL 3

(18)

How the program works

...and we shift to ...and we shift to column 4.

column 4.

There's still a There's still a conflict in

conflict in

column 4, so we column 4, so we try to shift

try to shift rightward rightward again...

again... ROW 1, COL 1

2 filled

ROW 2, COL 3 ROW 3, COL 4

(19)

How the program works

...but there's ...but there's

nowhere else to nowhere else to go.go.

ROW 1, COL 1

2 filled

ROW 2, COL 3 ROW 3, COL 4

(20)

How the program works

When we run out of When we run out of

room in a row:

room in a row:

pop the stack,pop the stack,

reduce reduce filled filled by 1by 1

and continue and continue working working

on the previous on the previous row.row.

ROW 1, COL 1

1 filled

ROW 2, COL 3

(21)

How the program works

Now we Now we continue continue

working on row working on row 2, shifting the 2, shifting the queen to the queen to the right.

right.

ROW 1, COL 1

1 filled

ROW 2, COL 4

(22)

How the program works

This position has This position has no conflicts, so no conflicts, so we can increase we can increase

filled

filled by 1, and by 1, and move to row 3.

move to row 3.

ROW 1, COL 1

2 filled

ROW 2, COL 4

(23)

How the program works

In row 3, we In row 3, we

start again at the start again at the first column.

first column.

ROW 1, COL 1

2 filled

ROW 2, COL 4 ROW 3, COL 1

(24)

Pseudocode for N-Queens

Initialize a stack where we can keep track of our Initialize a stack where we can keep track of our decisions.

decisions.

Place the first queen, pushing its position onto the Place the first queen, pushing its position onto the stack and setting

stack and setting filledfilled to 0 to 0..

repeat these stepsrepeat these steps

if there are no conflicts with the queens...if there are no conflicts with the queens...

else if there is a conflict and there is room to else if there is a conflict and there is room to shift the current queen rightward...

shift the current queen rightward...

else if there is a conflict and there is no room to else if there is a conflict and there is no room to shift the current queen rightward...

shift the current queen rightward...

(25)

Pseudocode for N-Queens

repeat these stepsrepeat these steps

if there are no conflicts with the queens...if there are no conflicts with the queens...

Increase filled by 1. If filled is now N, then the algorithm is done. Otherwise, move to

the next row and place a queen in the first column.

(26)

Pseudocode for N-Queens

repeat these stepsrepeat these steps

if there are no conflicts with the queens...if there are no conflicts with the queens...

else if there is a conflict and there is room to else if there is a conflict and there is room to shift the current queen rightward...

shift the current queen rightward...

Move the current queen rightward, adjusting the record on top of the stack

to indicate the new position.

(27)

Pseudocode for N-Queens

repeat these stepsrepeat these steps

if there are no conflicts with the queens...if there are no conflicts with the queens...

else if there is a conflict and there is room to else if there is a conflict and there is room to shift the current queen rightward...

shift the current queen rightward...

else if there is a conflict and there is no room else if there is a conflict and there is no room to shift the current queen rightward...

to shift the current queen rightward...

Backtrack!

Keep popping the stack, and reducing filled by 1, until you reach a row where the queen

can be shifted rightward. Shift this queen right.

(28)

Pseudocode for N-Queens

repeat these stepsrepeat these steps

if there are no conflicts with the queens...if there are no conflicts with the queens...

else if there is a conflict and there is room to else if there is a conflict and there is room to shift the current queen rightward...

shift the current queen rightward...

else if there is a conflict and there is no room else if there is a conflict and there is no room to shift the current queen rightward...

to shift the current queen rightward...

Backtrack!

Keep popping the stack, and reducing filled

by 1, until you reach a row where the queen can be shifted rightward. Shift this queen

right.

(29)

Stacks have many applications.Stacks have many applications.

The application which we have shown is called The application which we have shown is called backtracking

backtracking..

The key to backtracking: Each choice is recorded The key to backtracking: Each choice is recorded in a stack.

in a stack.

When you run out of choices for the current When you run out of choices for the current

decision, you pop the stack, and continue trying decision, you pop the stack, and continue trying

different choices for the previous decision.

different choices for the previous decision.

Summary

References

Related documents

Treatment retention and follow-up outcomes in the drug abuse treatment outcome

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

The PROMs questionnaire used in the national programme, contains several elements; the EQ-5D measure, which forms the basis for all individual procedure

information to reap an unfair benefit. Many of these donations were made at a time when it would have been illegal to make a sale of the same securities due to their access to this

Madeleine’s “belief” that she is Carlotta Valdez, her death and rebirth as Judy (and then Madeleine again) and Scottie’s mental rebirth after his breakdown.. All of these

This conclusion is further supported by the following observations: (i) constitutive expression of stdE and stdF in a Dam + background represses SPI-1 expression (Figure 5); (ii)

For the poorest farmers in eastern India, then, the benefits of groundwater irrigation have come through three routes: in large part, through purchased pump irrigation and, in a