Question: Right logic, bad output ( repeated ) public class Application { public static void main ( String [ ] args ) { House [ ]
Right logic, bad outputrepeated
public class Application
public static void mainString args
House houses new House;
if SolutionsolveRiddlehouses
System.out.printlnSolution found!";
Print the solution
for House house : houses
System.out.printlnhousecolor house.nationality house.drink house.cigar house.pet;
else
System.out.printlnNo solution found!";
class Solution
static String possibleColors red "green", "white", "yellow", "blue";
static String possibleNationalities Brit "Swede", "Dane", "Norwegian", "German";
static String possibleDrinks tea "coffee", "milk", "beer", "water";
static String possibleCigars Pall Mall", "Dunhill", "Blends", "Bluemasters", "Princes";
static String possiblePets dogs "birds", "cats", "horses", "fish";
public static boolean solveRiddleHouse houses, int index
if index
return true; Base case: all houses have been assigned attributes
Try all possible attributes for the current house
for String color : possibleColors
for String nationality : possibleNationalities
for String drink : possibleDrinks
for String cigar : possibleCigars
for String pet : possiblePets
housesindex new Housecolor nationality, drink, cigar, pet;
Check if the current assignment satisfies all constraints
if isValidhouses index && solveRiddlehouses index
return true; Found a valid solution
return false; No valid solution found, backtrack
public static boolean isValidHouse houses, int index
if index
return true; Reached the end of the houses array
Check if the house at index is null
if housesindex null
return false;
Apply the given clues as constraints
if index && housesindexnationality.equalsBrit && housesindexcolor.equalsred
return false;
if housesindexnationality.equalsSwede && housesindexpet.equalsdogs
return false;
if housesindexnationality.equalsDane && housesindexdrink.equalstea
return false;
if index && housesindexcolor.equalsgreen && housesindex null && housesindex color.equalswhite
return false;
if housesindexcolor.equalsgreen && housesindexdrink.equalscoffee
return false;
if housesindexcigar.equalsPall Mall" && housesindexpet.equalsbirds
return false;
if housesindexcolor.equalsyellow && housesindexcigar.equalsDunhill
return false;
if index && housesindexdrink.equalsmilk
return false;
if index && housesindexcigar.equalsBlends && housesindex null && housesindex pet.equalscats
return false;
if index && housesindexnationality.equalsNorwegian
return false;
Clue : The man who keeps horses lives next to the one who smokes Dunhills
if index && housesindexpet.equalshorses && housesindex null && housesindex cigar.equalsDunhill
return false;
Clue : The owner who smokes Bluemasters drinks beer
if housesindexcigar.equalsBluemasters && housesindexdrink.equalsbeer
return false;
Clue : The German smokes Princes
if housesindexnationality.equalsGerman && housesindexcigar.equalsPrinces
return false;
Clue : The Norwegian lives next to the blue house
if index && housesindexnationality.equalsNorwegian && housesindex null && housesindex color.equalsblue
return false;
return true;
class House
String color;
String nationality;
String drink;
String cigar;
String pet;
public HouseString color, String nationality, String drink, String cigar, String pet
this.color color;
this.nationality nationality;
this.drink drink;
this.cigar
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
