斑马难题Step by Step
在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的更多相关文章
- WPF Step By Step 系列 - 开篇 ·
WPF Step By Step 系列 - 开篇 公司最近要去我去整理出一个完整的WPF培训的教程,我刚好将自己学习WPF的过程和经验总结整理成笔记的方式来讲述,这里就不按照书上面的东西来说了,书本上 ...
- enode框架step by step之框架的物理部署思路
enode框架step by step之框架的物理部署思路 enode框架系列step by step文章系列索引: enode框架step by step之开篇 enode框架step by s ...
- Step by step Dynamics CRM 2011升级到Dynamics CRM 2013
原创地址:http://www.cnblogs.com/jfzhu/p/4018153.html 转载请注明出处 (一)检查Customizations 从2011升级到2013有一些legacy f ...
- Step by Step 创建一个新的Dynamics CRM Organization
原创地址:http://www.cnblogs.com/jfzhu/p/4012833.html 转载请注明出处 前面演示过如何安装Dynamics CRM 2013,参见<Step by st ...
- Step by step Install a Local Report Server and Remote Report Server Database
原创地址:http://www.cnblogs.com/jfzhu/p/4012097.html 转载请注明出处 前面的文章<Step by step SQL Server 2012的安装 &g ...
- Step by step Dynamics CRM 2013安装
原创地址:http://www.cnblogs.com/jfzhu/p/4008391.html 转载请注明出处 SQL Server可以与CRM装在同一台计算机上,也可安装在不同的计算机上.演示 ...
- Step by step 活动目录中添加一个子域
原创地址:http://www.cnblogs.com/jfzhu/p/4006545.html 转载请注明出处 前面介绍过如何创建一个域,下面再介绍一下如何在该父域中添加一个子域. 活动目录中的森林 ...
- SQL Server 维护计划实现数据库备份(Step by Step)(转)
SQL Server 维护计划实现数据库备份(Step by Step) 一.前言 SQL Server 备份和还原全攻略,里面包括了通过SSMS操作还原各种备份文件的图形指导,SQL Server ...
- 转:eclipse以及step into step over step return的区别
首先来讲一下step into step over step return的区别: step into就是单步执行,遇到子函数就进入并且继续单步执行:(F5) step over是在单步执行时,在函数 ...
随机推荐
- 19)PHP,数组知识
(1)数组的基础 在PHP中,数组的下标可以是数字,也可以是字符串 在PHP中,数组元素的顺序不是由下标决定的,而是由其加入的的顺序决定 (2)数组定义: array(1,5,11,'abs',tru ...
- Kubernetes系列二: 使用kubeadm安装k8s环境
环境 三台主机,一台master,两台node 作为master 作为node节点 作为node节点 每台主机Centos版本使用 CentOS Linux release 7.6.1810 (Cor ...
- JavaScript 的 URL 对象是什么?
如果我们自己编写从URL中分析和提取元素的代码,那么有可能会比较痛苦和麻烦.程序员作为这个社会中最“懒”的群体之一,无休止的重复造轮子必然是令人难以容忍的,所以大多数浏览器的标准库中都已经内置了URL ...
- Opencv笔记(九)——图像阈值
学习目标: 学习简单阈值,自适应阈值,Otsu's 二值化等 学习函数cv2.threshold,cv2.adaptiveThreshold 等. 一.简单阈值 与名字一样,这种方法非常简单.但像素值 ...
- sm4 加解密示例
int do_crypt(char *outfile) { unsigned ]; int outlen, tmplen; /* Bogus key and IV: we'd normally set ...
- windows10使用npm安装vue、vue-cli
从网上下载了一个免费的vue.js前端模板,准备和Django整合出一个项目出来,然后发现前端代码都是.vue文件,已经整合过.html,很容易,感觉这个.vue的前端稍微复杂一些 本文主要参考博客及 ...
- centos7_安装jira7
centos7 安装jira7 参考文档:http://www.cnblogs.com/kevingrace/p/7608813.html 1.准备环境 centos7 memory:8G stora ...
- 场景实践篇二:Nginx作为缓存
cd /etc/nginx/conf.d/ vim cache_test.conf
- Codeforces Round#615 Div.3 解题报告
前置扯淡 真是神了,我半个小时切前三题(虽然还是很菜) 然后就开始看\(D\),不会: 接着看\(E\),\(dp\)看了半天,交了三次还不行 然后看\(F\):一眼\(LCA\)瞎搞,然后\(15m ...
- ORs-4-Enhanced Role of OR Gene Loss (Pseudogenization) in Birds
Enhanced Role of OR Gene Loss (Pseudogenization) in Birds 1.因为文献已经证明(a)基因缺失和得到对于进化有影响,(b)大的基因家族对进化影响 ...