0% found this document useful (0 votes)
10 views

Homework 09

useful

Uploaded by

zhangkaiqi666666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Homework 09

useful

Uploaded by

zhangkaiqi666666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Fundamentals of Computer

Science
Homework Set 9
December 20, 2023

1. (4’) In this question, we use a breadth-first approach to


solve eight-puzzle problem.
You should first read the following program implementation, and
then fill the output of the program in the form. The status should
be printed in row major, with the blank cell replaced by ‘0’. For
example, the initial status can be expressed as "123405786"
without quotes

procedure BFS_Eight_Puzzle(InitState)
if InitState is the target state:
print “end” and return
Q <= initialize an empty queue
push the InitState into Q
print InitState in row-major order
while (Q is not empty) do:
S <= pop an element from Q 1 2 3
for D in [Left, Down, Right, Up] sequentially:
if there is such a block which can move D: 4 5
S’ <= apply D to the block
if S’ has not been pushed to Q in the past: 7 8 6
print S’ in row-major order
if S’ is the target state:
print “end”
delete Q and return
else:
push S’ into Q

Example Output (from the initial state “123456708”)

Line 1 123456708

Line 2 123456780

Line 3 end

Line 4
Your Answer

Line 1 123405786

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8 end

Line 9

Line 10

Line 11

Line 12

Line 13

Line 14

Line 15

Line 16
2. (6’) In this question, we use the number of out-of-place
tiles as a heuristic to solve eight-puzzle problem.
You should first read the following program implementation, and
then fill the output of the program in the form. The status should
be printed in row major, with the blank cell replaced by ‘0’. For
example, the initial status can be expressed as "123408765"
without quotes.
In this program, we use a data structure named priority queue,
which can pop the element with the highest priority. In computer
science, data structures such as heap, self-balancing binary
search tree, etc. can all be implemented as priority queue. When
comparing two states, the state with the smaller heuristic (i.e.,
number of out-of-place tiles) has a higher priority. If the
heuristic of two states are equal, the state that was pushed into
the priority queue later has a higher priority.

procedure A*_Eight_Puzzle(InitState)
if InitState is the target state:
print “end” and return
Q <= initialize an empty priority queue
push the InitState into Q
print InitState in row-major order
while (Q is not empty) do:
S <= pop the element with the highest priority
from Q
for D in [Left, Down, Right, Up] sequentially: 1 2 3
if there is such a block which can move D:
S’ <= apply D to the block 4 8
if S’ has not been pushed to Q in the past:
print S’ in row-major order 7 6 5
if S’ is the target state:
print “end”
delete Q and return
else:
push S’ into Q
Your Answer

Line 1 123408765

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Line 11

Line 12

Line 13

Line 14

Line 15 end

Line 16

Line 17

You might also like