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.
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:
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)
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 CommentsShow comments
Problem Recent Solvers49