Write a program to solve the “Vegetarians and Meat Eaters” problem. Three vegetarians and three hungry meat-eaters need to cross a river. Unfortunately, the boat only holds two people. If the meat-eaters outnumber the vegetarians on either bank, the vegetarians will be eaten! The computer must find a series of moves that gets all three vegetarians and all three meat-eaters across the river safely. I know you can solve the problem! Can the computer? You must give the computer some representation of the problem and the choices available. The computer must find the solution. You are given broad leeway on how to represent this problem. Recursion and backtracking (depth-first search) is one way to solve this.
The base cases are:
3 vegetarians and 3 meat eaters on left side of river = success
3 vegetarians and 3 meat eaters on right side of river = we’re back at the start and have looped around, stop following this path
More meat eaters than vegetarians on left side of river = vegetarians get eaten More meat eaters than vegetarians on right side of river = vegetarians get eaten
Variables are M and V i.e [1M,2M,3M, 1V, 2V, 3V]
People start on right bank, end up on left bank
• Board state tracking/output
• Moves change state of board
• Computer moves search
• Program finds correct solution
Executable code
//VegetariansAndMeatEaters.java
import java.util.Scanner;
//Class
public class VegetariansAndMeatEaters
{
//Method CheckOn()
public static boolean CheckOn(String s)
{
//Condition check
if (s.indexOf("m")< 0 )
{
//Condition
check
if
((s.indexOf("g")>-1 && s.indexOf("t")>-1) ||
(s.indexOf("g"))>-1 && s.indexOf("r")>-1)
{
//Display
System.out.println("Game Over");
//Return
return false;
}
}
//Return
return true;
}
public static void show(String s1,String s2)
{
//Display
System.out.println(s1);
System.out.println(s2);
}
//Driver
public static void main(String[] args)
{
//Create instance
Scanner scIn=new
Scanner(System.in);
//Variables
int userChoice;
String b1="tgrm";
String b2="";
boolean flagger=true;
//Do while
do
{
//Display
System.out.println("\nMenu operations");
System.out.println("1:Transfer goat");
System.out.println("2:Transfer grass");
System.out.println("3:Transfer tiger");
System.out.println("4:Transfer man\n");
//Read user
input
userChoice=scIn.nextInt();
//Switch
case
switch(userChoice)
{
//Case 1
case 1:
//Condition check
if(b1.indexOf("m")>0)
{
//Update
b1=b1.replace("g", "");
b2=b2+"g";
b1=b1.replace("m", "");
b2=b2+"m";
//Function
call
flagger=CheckOn(b1);
}
//Otherwise
else
{
//Update
b2=b2.replace("g", "");
b1=b1+"g";
b2=b2.replace("m", "");
b1=b1+"m";
//Function
call
flagger=CheckOn(b2);
}
//Function call
show(b1,b2);
//Stop
break;
//Case 2
case 2:
//Condition check
if(b1.indexOf("m")>0)
{
//Update
b1=b1.replace("r", "");
b2=b2+"r";
b1=b1.replace("m", "");
b2=b2+"m";
//Function
call
flagger=CheckOn(b1);
}
//Otherwise
else
{
//Update
b2=b2.replace("r", "");
b1=b1+"r";
b2=b2.replace("m", "");
b1=b1+"m";
//Function
call
flagger=CheckOn(b2);
}
//Function call
show(b1,b2);
//Stop
break;
//Case 3
case 3:
//Condition check
if(b1.indexOf("m")>0)
{
//Update
b1=b1.replace("t", "");
b2=b2+"t";
b1=b1.replace("m", "");
b2=b2+"m";
//Function
call
flagger=CheckOn(b1);
}
//Otherwise
else
{
//Update
b2=b2.replace("t", "");
b1=b1+"t";
b2=b2.replace("m", "");
b1=b1+"m";
//Function
call
flagger=CheckOn(b2);
}
//Function call
show(b1,b2);
//Stop
break;
//Case 4
case 4:
//Condition check
if(b1.indexOf("m")>0)
{
//Update
b1=b1.replace("m", "");
b2=b2+"m";
}
//Otherwise
else
{
b2=b2.replace("m", "");
b1=b1+"m";
}
//Function call
show(b1,b2);
//Stop
break;
}
//Condition
check
if(b1.equals(""))
{
//Display
System.out.println("you won");
//Stop
break;
}
}
//End of do while
while(flagger);
}
}
4. Comprehensive (20 points) Based on Goodrich Programming Projects 12.1. Write a program to solve the "Vegetarians and Meat Eaters" problem. Three vegetarians and three hungry meat-eaters need to cross a river. Unfortunately, the boat only holds two people. If the meat- eaters outnumber the vegetarians on either bank, the vegetarians will be eaten! Please note: nobody gets to stay on the boat. When there's three meat-eaters on one side, it doesn't matter if one just came over on the...
I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks The game of ‘Walls’...
Discrete Math and Computer Science I need help with #2 the programming part is in C++ Thank you! Main topic and problems for the final project The main purpose of the project is to introduce you how to use a computer as a research tool in an Introductory Discrete Mathematics. In this project you will be asked to show how the Fibonacci sequence (F,) is related to Pascal's triangle using the following identities by hand for small n and then...
Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...
Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 { public static void main( String [] args ) { //N denoting the size of the board int n; ...
Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 { public static void main( String [] args ) { //N denoting the size of the board int...
Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...
Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. PROBLEM 1 INFORMATION IF YOU NEED IT AS WELL: Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem3. CODE PROVIDED FOR PROBLEM 2: import java.util.Scanner; public class Problem2 { public static void main( String [] args ) { //N...
You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...
Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...