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;
}

代码



最新文章

  1. web项目ajax技术一些总结
  2. Java.lang.NoSuchFieldError: INSTANCE异常
  3. Python 中的进程、线程、协程、同步、异步、回调
  4. c#.net 访问SQL SERVER 时提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏
  5. MySQL防注入[待续]
  6. Apache Spark GraphX的使用简介
  7. 重温XML
  8. tar linux 打包 压缩 gzip 命令说明
  9. T_SQL编程赋值、分支语句、循环
  10. Git详解及 github与gitlab使用
  11. JS题目
  12. Remastersys打包你自己的ubuntu成iso文件
  13. 20155208徐子涵 Exp 6 信息搜集与漏洞扫描
  14. Hibernate中报错org.hibernate.HibernateException: No CurrentSessionContext configured!
  15. Oracle数据库实例的启动及关闭
  16. 设置DataGridView中表头颜色
  17. Linux记录屏幕输出log
  18. httpclient 连接保持
  19. hdu5228
  20. JQuery 导入导出 Excel

热门文章

  1. The website is API(3)
  2. $identify 的 “identify” 表示一个Perl标识符,即 identifier
  3. sql的书写顺序
  4. IP命令介绍
  5. 熊海CMS_1.0 代码审计
  6. DataGrip设置时区
  7. redis中间件
  8. JavaScript 简介与语法
  9. 非参数检验|Sign test|Wilcoxon signed rank test|Wilcoxon rank sum test|Bootstrapping
  10. WWT在中国:一个改变了人类探索宇宙方式的少年梦想