Albert Einstein supposedly said that 98% of the people in the world couldn't solve this one:
There are 5 houses sitting next to each other, each with a different color, occupied by 5 guys, each from a different country, and with a favorite drink, cigarette, and pet. Here are the facts:
The question is: Who owns the fish?
This is really a fun puzzle.
When my friend first showed me the puzzle I was totally confused(i am not that smart to understand in the first place) but later I solved the puzzle and three days later I developed a C Program that can solve the puzzle. I did it around 3 years ago and right now I don't have the source code either (lost with hard disk formatting :) ) No paper for it either :)
But Finally I decide to write it somewhere. I choose bytes.com:)
Lets start from how can we make computer understand this problem. It is obvious that We don't have that smart API to read and explain the text. So I broke the information. I made a XML file to hold the statements. The XML file was like below(when I did it, I was in university. I did not have any clear Idea about how to work with XML File).
This format can be understandble by machine :)
Now if I simplay say how to solve from it is: Backtracking algorithm
Solution
Well that is. If you have any doubt or need to know anything. Please let me know
There are 5 houses sitting next to each other, each with a different color, occupied by 5 guys, each from a different country, and with a favorite drink, cigarette, and pet. Here are the facts:
- The British occupies the red house.
- The Swedish owns a dog.
- The Danish drinks tea.
- The green house is on the left of the white house.
- The owner of the green house drinks coffee.
- The person who smokes "Pall Mall" owns a bird.
- The owner of the yellow house smokes "Dunhill".
- The owner of the middle house drinks milk.
- The Norwegian occupies the 1st house.
- The person who smokes "Blend" lives next door to the person who owns a cat.
- The person who owns a horse live next door to the person who smokes "Dunhill".
- The person who smokes "Blue Master" drinks beer.
- The German smokes "Prince".
- The Norwegian lives next door to the blue house.
- The person who smokes "Blend" lives next door to the person who drinks water.
The question is: Who owns the fish?
This is really a fun puzzle.
When my friend first showed me the puzzle I was totally confused(i am not that smart to understand in the first place) but later I solved the puzzle and three days later I developed a C Program that can solve the puzzle. I did it around 3 years ago and right now I don't have the source code either (lost with hard disk formatting :) ) No paper for it either :)
But Finally I decide to write it somewhere. I choose bytes.com:)
Lets start from how can we make computer understand this problem. It is obvious that We don't have that smart API to read and explain the text. So I broke the information. I made a XML file to hold the statements. The XML file was like below(when I did it, I was in university. I did not have any clear Idea about how to work with XML File).
Code:
<statements> <statement>nation=british&color=red</statement> //& sign explain that it is about same person <statement>nation=Sweden&pet=dog</statement> <statement>nation=Denish&drink=tea</statement> <statement>color=white>color=green</statement> // > sign mean they are about different perosn and leve one after another <statement>smoke="pall mall"&pet=bird</statement> <statement>color=yellow&smoke=dunhill</statement> <statement>position=3&drink=milk</statement> //I have assigned [b]position[/b] for fixed position <statement>nation=norway&position=1</statement> <statement>smoke=blend+pat=cat</statement> // + sign tells they live next to each other but order is undefined <statement>smoke=Dunhill+pet=horse</statement> <statement>smoke="blue Master"&drink=beer</statement> <statement>nation=german&smoke=prince</statement> <statement>nation=norway+color=blue</statement> <statement>smoke=blend+drink=water</statement> </statements> <search>pet=fish</search>
Now if I simplay say how to solve from it is: Backtracking algorithm
Solution
Code:
Function main Begin read the xml and parse each state ment in accordance field set all the fixed position datas COMMENT START the generated table is ------------------------------------------------------------ nation |color |pet |drink |Smoke ------------------------------------------------------------ 1 norway | | | | ------------------------------------------------------------ 2 | | | | ------------------------------------------------------------ 3 | | |milk | ------------------------------------------------------------ 4 | | | | ------------------------------------------------------------ 5 | | | | ------------------------------------------------------------ COMMENT END Remove all fiexed statement from the list Call Recursion_solve(1, total_statement) End Function Recursion_solve(current_statement_number, total_statement) if current_statement_number>total_statement return true get statement type if statement type is & begin if Check if any part of the statement is already in the table If any part is available Begin if try to set rest of the part in the table COMMENT BEGIN COMMENT END if one part is available and other part missmatch Begin IF return false END IF else if one part is avail and other part match or empty BEgING IF set rest of the parts return Recursion_solve(current_statement_number+1,total_statement) END IF end if else if no part is available Begin if for i=1 to Total_row //in this case 5 Begin For is Row(i) available? if availalbe Row(i) for all parts Begin If set all the part in the table Ret_value = Recursion_solve(current_statement_number+1,total_statement) if Ret_valeu = true BEGIN IF return Ret_value END IF End If if i=Total_row Begin If return false End IF End For End if end if else if statement type is > get current_statement using current_statement_number return Solution_for_>(current_statement, total_statement) end if else if statement type is + return Solution_for_+(current_statement_number) End Function Solution_for_+(current_statement_number) convert type "+" statement to type ">" statement Commetn BEGIN we will get 2 statemetnt for each + statement as example <statement>smoke=Dunhill+pet=horse</statement> will be <statement>smoke=Dunhill>pet=horse</statement> and <statement>pet=horse>smoke=Dunhill</statement> COMMENT END foreach tyep ">" statement begin foreach Ret_value=Solution_for_>(statement) if(Ret_value==true) return true; end foreach return false Begin End Function Solution_for_>(current_statement) //the design patter of the function is it must return a value Begin check if part 1 is available in the table if part1 is avilable Begin if get row number if row+1 > Total_row retrun false; check if part2 is empty or similar at row+1 Begin If set part2 in row+1 return Recursion_solve(current_statement_number+1,total_statement) End If else if part2 is not empty and missed match return false; end if else if part2 is available begin if get row number if row == 1 retrun false; check if part1 is empty or similar at row-1 Begin If set part1 in row-1 return Recursion_solve(current_statement_number+1,total_statement) End If else if part1 is not empty and missed match return false; endif eles if both part1 and part 2 is absent begin if for i = 1 to Total_row-1 Begin For check if part1 and part 2 can be set in row(i) and row(i+1) sequntially Begin If Ret_value=Recursion_solve(current_statement_number+1,total_statement) if (Ret_value==true) return true End if End For if row==Total_row-1 return false End If // program will never reach to this statement return false; End