Write a java recursive program to solve the "Missionaries and Cannibals" problem:Three missionaries and three cannibals come to a river and find a boat that holds two. If the cannibals ever outnumber the missionaries on either bank, the missionaries will be eaten. How might they cross safely? Input a character from the keyboard either a c (cannibal) or m (missionary). Check case and error check inputs. The output should include the initial problem, the moves you make, and a "picture" of the current state of the puzzle after each move (round). The final output should include the total number of rounds needed to solve the problem. Finally, ask the user if they wish to run the program again. The program must use recursion as part of the solution. Refer to the sample output below.
Write a java recursive program to solve the "Missionaries and Cannibals" problem:Three missionaries and three cannibals come to a river and find a boat that holds two. If the cannibals ever outnumber the missionaries on either bank, the missionaries will be eaten. How might they cross safely? Input a character from the keyboard either a c (cannibal) or m (missionary). Check case and error check inputs. The output should include the initial problem, the moves you make, and a "picture" of the current state of the puzzle after each move (round). The final output should include the total number of rounds needed to solve the problem. Finally, ask the user if they wish to run the program again. The program must use recursion as part of the solution. Refer to the sample output below.
Sample Run (partial):Crossing the River-----------------------------------------Round 0Left RightMMMCCCBoatFirst passenger (c for cannibal, m for missionary): HIllegal Input!First passenger (c for cannibal, m for missionary): mSecond passenger (c for cannibal, m for missionary, n for none): c-----------------------------------------Round 1Left RightMMCC MC BoatFirst passenger (c for cannibal, m for missionary): CSecond passenger (c for cannibal, m for missionary, n for none): cIllegal input!Second passenger (c for cannibal, m for missionary, n for none): n-----------------------------------------Round 2Left RightMMCCC M BoatMissionaries eaten by cannibals! You lose!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps