斑马难题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是在单步执行时,在函数 ...
随机推荐
- 33)new和delete关键字
---------------------------------------------------------------------------------------------------- ...
- [LC] 90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- D - Project Presentation(DFS序+倍增LCA)
You are given a tree that represents a hierarchy in a company, where the parent of node u is their d ...
- D. Fish eating fruit
题:https://nanti.jisuanke.com/t/41403 题意:求任意俩点之间距离之和模3后的三个结果的总数(原距离之和) 第一种做法: 树形dp #include<bits/s ...
- 从二叉搜索树到AVL树再到红黑树 B树
这几种树都属于数据结构中较为复杂的,在平时面试中,经常会问理解用法,但一般不会问具体的实现,所以今天来梳理一下这几种树之间的区别与联系,感谢知乎用户@Cailiang,这篇文章参考了他的专栏. 二叉查 ...
- 《C程序设计语言》练习1-5
#include<stdio.h> /*修改温度转换程序,要求以逆序(即按照从300度到0度的顺序)打印温度转换表*/ main () { float fahr,celsius; int ...
- 学习python-20191208(2)-Python Flask高级编程开发鱼书_第03章_数据与flask路由
视频06: 定义静态方法的两种方式: 1.在方法上方加上装饰@staticmethod 2.在方法上方加上装饰@classmethod 方法中要加参数cls 如:def search_by_isb ...
- Android内存调优的一些方法
一.巧妙使用软引用和弱引用 软引用,系统内存紧张时会回收软引用对象,一般用用高速缓存,例如图片缓存,我们一般通过内存缓存图片来提高图片加载速度,但内存吃紧的时候可以通过软引用及时对图片资源回收. 弱引 ...
- JavaScript学习总结(八)正则表达式
转自:http://segmentfault.com/a/1190000000699097 基本概念 正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”) ...
- JSP_EL的回顾
在 JSP 页面中,使用标签库代替传统的 Java 片段语言来实现页面的显示逻辑已经不是新技术了,然而,由自定义标签很容易造成重复定义和非标准的实现.鉴于此,出现了 JSTL ( JSP Standa ...