Problem 1179. Knights and Knaves (part 1)
This is a Matlab adaptation of the Knights and Knaves logical puzzles.
You are in an island where all inhabitants are either Knights, who always tell the truth, or Knaves, who always lie. The island inhabitants can always tell Knights and Knaves apart by their appearance, but to you, as an outsider, they look exactly the same.
Upon arriving to this island you encounter two inhabitants standing at a fork in the road. One of them is a Knight, and the other is a Knave, but you do not know which. One of the roads will lead you to the castle, where you wish to go. Your job is to ask one or several yes/no questions that will allow you to know which one is the correct road.
Implementation
You function will take two function handles as inputs (one per inhabitant), and must return a single scalar indicating the correct road (1 for the first road or 2 for the second road):
function correct_road = solver(fA, fB)
You may ask one inhabitant a question by evaluating his associated function handle on a char string (the 'question'). Strings must be a valid matlab commands that when evaluated return a scalar logical value (yes/no questions, where true is a yes, and false is a no). Strings may refer to the following variables:
- A: true if the first inhabitant is a Knight, false if he is a Knave
- B: true if the second inhabitant is a Knight, false if he is a Knave
- X: 1 if the first road leads to the castle, 2 if the second road leads to the castle
Asking questions, examples:
x=fA('A==true');
asks the first inhabitant whether he is a Knight (note: this returns always true, since both Knights and Knaves would tell you they are Knights; remember, Knaves always lie)
x=fB('X==1');
asks the second inhabitant whether the first road leads to the castle (not particularly useful by itself since we do not know whether he is going to respond truthfully or not)
Next problem in this series: Knights and Knaves (part 2)
Solution Stats
Problem Comments
-
2 Comments
I remembered this cartoon by reading the problem:
http://xkcd.com/246/
:D
I'll have to think how to incorporate the stabbing in the problem... :)
Solution Comments
Show commentsProblem Recent Solvers52
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
49043 Solvers
-
2718 Solvers
-
389 Solvers
-
8850 Solvers
-
Number of Even Elements in Fibonacci Sequence
1267 Solvers
More from this Author38
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!