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 commentsGroup

Paper-&-pencil Games
- 19 Problems
- 4 Finishers
- Right Triangle Side Lengths (Inspired by Project Euler Problem 39)
- Calculate the area of a triangle between three points
- Right Triangle Side Lengths (Inspired by Project Euler Problem 39)
- Television Screen Dimensions
- Find third Side of a right triangle given hypotenuse and a side. No * - or other functions allowed
- Calculate the area of a triangle between three points
- Are all the three given point in the same line?
- Volume of a Parallelepiped
- Spherical radius given four points
- Angle between Two Vectors
- Is the Point in a Circle?
- What is the distance from point P(x,y) to the line Ax + By + C = 0?
- Covering area
- Clock Hand Angle 1
- Angle between two vectors
- Similar Triangles - find the height of the tree
- Circumscribed circles
- Are you in or are you out?
- Clockwise or Counterclockwise
- Smallest distance between a point and a rectangle
- Great Circle Distance
- Right and wrong
Problem Recent Solvers52
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!