Question: This code results in No solution found, which is not correct. I just need help with the logic. public class House { String color; String
This code results in No solution found, which is not correct. I just need help with the logic.
public 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 cigar;
this.pet pet;
public 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
Apply the given clues as constraints
Clue : The Brit lives in the red house
if housesindexnationality.equalsBrit && housesindexcolor.equalsred
return false;
Clue : The Swede keeps dogs as pets
if housesindexnationality.equalsSwede && housesindexpet.equalsdogs
return false;
Add more constraints for the remaining clues
Clue : The dane drinks tea
if housesindexnationality.equalsDane && housesindexdrink.equalstea
return false;
Clue : The green house is on the left of and next to the white house
ifhousesindexcolor.equalsgreen && housesindex color.equalswhite
return false;
Clue : The green house owner drinks coffee
ifhousesindexcolor.equalsgreen && housesindexdrink.equalscoffee
return false;
Clue : The person who smokes Pall Malls keeps birds
ifhousesindexcigar.equalsPall Mall" && housesindexpet.equalsbirds
return false;
Clue : The owner of the yellow house smokes Dunhills
ifhousesindexcolor.equalsyellow && housesindexcigar.equalsDunhill
return false;
Clue : The man living in the house right in the center drinks milk
ifindex && housesindexdrink.equalsmilk
return false;
Clue : The man who smokes Blends lives next to the one who keeps cats
ifhousesindexcigar.equalsBlends && housesindex pet.equalscats
return false;
Clue : The Norwegian lives in the first house
ifindex && housesindexnationality.equalsNorwegian
return false;
Clue : The man who keeps horses lives next to the one who smokes Dunhills
ifhousesindexpet.equalshorses && housesindex cigar.equalsDunhill
return false;
Clue : The owner who smokes Bluemasters drinks beer
ifhousesindexcigar.equalsBluemasters && housesindexdrink.equalsbeer
return false;
Clue : The German smokes Princes
ifhousesindexnationality.equalsGerman && housesindexcigar.equalsPrinces
return false;
Clue : The Norwegian lives next to the blue house
ifhousesindexnationality.equalsNorwegian && housesindex color.equalsblue
return false;
Clue : The man who smokes Blends has a neighbor who drinks water
ifhousesindexcigar.equalsBlends && housesindex drink.equalswater
return false;
return true;
public static void mainString args
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
