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
-
5910 Solvers
-
Find best placement for ordered dominoes (harder)
309 Solvers
-
Find the maximum number of decimal places in a set of numbers
2861 Solvers
-
623 Solvers
-
Sum the numbers on the main diagonal
582 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!