Question: Write the Python code necessary to solve the Hobbits and Orcs problem Hobbits and Orcs: Three hobbits and three orcs are sitting on the left

Write the Python code necessary to solve the Hobbits and Orcs problem

Hobbits and Orcs: Three hobbits and three orcs are sitting on the left bank of a river. Conveniently, there also is a boat on the left bank. All hobbits and orcs are to reach the right river bank by boat. The boat can cross the river in either direction with one or two creatures inside (it cannot go empty). When orcs are in the majority, they will eat the hobbits. Therefore, the possible movements are constrained by the requirement that at any time, the number of orcs never outnumber the number of hobbits. What is the minimum number of boat trips, and what are the respective boat loads, that will ferry the three hobbits and the three orcs from the left bank to the right bank?

This exercise will consist of your implementing the following functions (with exactly these names): goal_fct(state) successor_fct(state) eval_fct(state) show_state(state)

Your coding the for hobbits and orcs will depend on your representation of a problem state. How will you present the fact that the j1 hobbits and k1 orcs are on the left bank, j2 hobbits and k2 orcs are on the right, and the boat is on the left bank? The below is just an example of how one could go about representing states and produce successor states: START = [[3,3],[0,0],'left'] GOAL = [[0,0], [3,3], 'right'] def successor_fct(state): succs = [] if state[2] == 'right': succs.append(hh_2left(state)) succs.append(o_2left(state)) else: succs.append(hh_2right(state)) succs.append(oo_2right(state)) succs = [s for s in succs if s != None] # remove all None return succs # 2 hobbits go right to left; +2 hobs on left, -2 hobs on right; # watch for danger on right! def hh_2left(state): state2 = copy.deepcopy(state) [[h1,o1],[h2,o2],b] = state2 if b == 'left' or h2 < 2 or 0 < h2-2 < o2: return None # no legal successor exists return [[h1+2, o1],[h2-2,o2],'left'] # follow with analogous functions until you cover all possible boat # loads of 1-2 hobbits, 1-2 orcs, or 1 hobbit and 1 orcs moving # left to right or right to left.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!