码迷,mamicode.com
首页 > 其他好文 > 详细

斑马难题Step by Step

时间:2020-02-04 14:12:47      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:ima   highlight   rmi   lan   分析   OLE   water   ring   data-   

exercism.io被这个 Zebra Puzzle 难住了。这里一步一步的解决。。。

1.There are five houses.
2.The Englishman lives in the red house.
3.The Spaniard owns the dog.
4.offee is drunk in the green house.
5.The Ukrainian drinks tea.
6.The green house is immediately to the right of the ivory house.
7.The Old Gold smoker owns snails.
8.Kools are smoked in the yellow house.
9.Milk is drunk in the middle house.
10.The Norwegian lives in the first house.
11.The man who smokes Chesterfields lives in the house next to the man with the fox.
12.Kools are smoked in the house next to the house where the horse is kept.
13.The Lucky Strike smoker drinks orange juice.
14.The Japanese smokes Parliaments.
15.The Norwegian lives next to the blue house.

fang 求 Water 的房间和 Zebra 的房间

分析

首先,每个房间有 5 个属性,加上上面的若干条件限制。

属性

color  --  $red, $green, $ivory, $yellow, $blue
Nationality (国家)  --  $Englishman, $Spaniard, $Ukrainian, $Norwegian, $Japanese
Drink -- $coffee, $tea, $milk, $oj, $WATER
Smoke -- $OldGold, $Kools, $Chesterfields, $LuckyStrike, $Parl 大专栏  斑马难题Step by Stepiaments
Pet -- $dog, $snail, $fox, $horse, $ZEBRA

需要在满足所有条件的情况下讲这五个属性分给五个房间,大概就是这么一个思路。 那么我们一共有 5! ** 5 种情况。

先将房间和这些属性都抽象成 1,2,3,4,5

这里拿第二个条件说,就是 Englishman == red,因为他们都属于一个房间的属性,所以值必然相等。

当然,还有 6 , 11 这种条件,我们只需要加上两个方法即可。

/**
 * Determinate the right of relation.
 *
 * @param  int $n
 * @param  int $m
 *
 * @return boolean
 */
function rightOf($n, $m) {
    return $n == $m + 1;
}
 
/**
 * Determinate the neighbouring relations
 *
 * @param  int $n
 * @param  int $m
 *
 * @return boolean
 */
function nextTo($n, $m) {
    return abs($n - $m) == 1;
}

代码



斑马难题Step by Step

标签:ima   highlight   rmi   lan   分析   OLE   water   ring   data-   

原文地址:https://www.cnblogs.com/lijianming180/p/12258985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!