斑马难题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是在单步执行时,在函数 ...
随机推荐
- Android开发学习4
学习内容: 1.RecyclerView 2.WebView
- Kubernetes 问题定位技巧:分析 ExitCode
使用 kubectl describe pod 查看异常的 pod 的状态,在容器列表里看 State 字段,其中 ExitCode 即程序退出时的状态码,正常退出时为0.如果不为0,表示异常退出,我 ...
- java.lang.IllegalArgumentException: Cannot format given Object as a Date
在进行日期转换的时候遇到了这个问题, 非常的恼火 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ...
- MFC的程序,不想显示窗口,任务栏里也不显示
在dialog的oninitdialog里设置如下属性,很简单,网上一些乱七八糟的做法,一行代码就能搞定啊 SetWindowPos(&CWnd::wndNoTopMost,0,0,0,0,S ...
- Complier
Complier [2019福建省赛] 模拟题应该有信心写,多出一些样例 当/* 与// 在一起的时候总会出错,一旦出现了这些有效的 应该把它删掉不对后面产生影响 #include<bits/s ...
- iOS之正则表达式(一)
这几天研究正则表达式发现正则表达式真是个好东西,可以在支付的时候轻松匹配交易数额,入门内容以及匹配如下: @interface ViewController () @property (weak, n ...
- 关于 Xpath 定位
关于 Xpath 定位 问: // 和 / 的区别 表达式 描述 nodename 选取此节点的所有子节点. / 从根节点选取. // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置. . ...
- Codeforces Round #576 (Div. 2) D. Welfare State
http://codeforces.com/contest/1199/problem/D Examples input1 output1 input2 output2 Note In the firs ...
- 文件流下载时 axios blob文件大小不正确?
文件流下载时 js blob文件大小不正确? res.data的字节长度 length blob.size匹配不上.. axio请求里必须修改 responseType: 'blob' 参数, 默认是 ...
- J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)
Rar the Cat((™)) is the founder of ACM (Association of Cats and Magical Lights). This is the story o ...