一道题“谁养鱼”的穷举解法。

通过编程解决一道复杂的逻辑谜题,涉及国籍、房屋颜色、饮料、香烟品牌及宠物的匹配问题,找出养鱼的人。

有一道题是这样的:
  1、在一条街上,有5座房子,喷了5种颜色。
        2、每个房里住着不同国籍的人
  3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物
  问题是:谁养鱼?

  提示:
  1、英国人住红色房子
  2、瑞典人养狗
  3、丹麦人喝茶
  4、绿色房子在白色房子左面
  5、绿色房子主人喝咖啡
  6、抽Pall Mall 香烟的人养鸟
  7、黄色房子主人抽Dunhill 香烟
  8、住在中间房子的人喝牛奶
  9、 挪威人住第一间房
  10、抽Blends香烟的人住在养猫的人隔壁
  11、养马的人住抽Dunhill 香烟的人隔壁
  12、抽Blue Master的人喝啤酒
  13、德国人抽Prince香烟
  14、挪威人住蓝色房子隔壁
  15、抽Blends香烟的人有一个喝水的邻居

想到自己的脑子不够用,还是求助于计算机吧。所以用程序实现如下:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
namespace FiveFive
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif在一条街上,有5座房子,喷了5种颜色。 
InBlock.gif  2、每个房里住着不同国籍的人 
InBlock.gif  3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物 
InBlock.gif  问题是:谁养鱼? 
InBlock.gif
ExpandedSubBlockEnd.gif * 
*/

InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//这一步构造问题
InBlock.gif
            Question q = new Question();
InBlock.gif
InBlock.gif            
//本步打印结果呀
InBlock.gif
            q.CalcResult();
InBlock.gif
InBlock.gif            Console.WriteLine(
"finished");
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static string ConvertIntString(int v)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return v.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class ArrayGroupState<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private List<int[]> _cache = null;
InBlock.gif        
private static Dictionary<int, List<int[]>> _cachecache = new Dictionary<int, List<int[]>>();
InBlock.gif        
private int _currentState = 0;
InBlock.gif
InBlock.gif        
public int[] State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _cache[this._currentState]; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ArrayGroupState(T[] array)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
this.InitCache(array.Length);
InBlock.gif
InBlock.gif            _currentState 
= 0;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int[] GetNextState(int[] stateObject)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//产生一个新的组合
InBlock.gif
            int[] state = new int[stateObject.Length];
InBlock.gif            stateObject.CopyTo(state, 
0);
InBlock.gif
InBlock.gif            
for (int i = state.Length - 1; i >= 0--i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i >= state.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int[] ar = new int[state.Length - i];
InBlock.gif                    Array.Copy(state, i, ar, 
0, ar.Length);
InBlock.gif                    Array.Sort(ar);
InBlock.gif
InBlock.gif                    
if (state[i] == ar[ar.Length - 1])
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
continue;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
int pos = Array.BinarySearch(ar,state[i]);
InBlock.gif                        
++pos;
InBlock.gif                        state[i] 
= ar[pos];
InBlock.gif
InBlock.gif                        
for (int j = 0; j < ar.Length; ++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (j == pos)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
continue;
ExpandedSubBlockEnd.gif                            }

InBlock.gif
InBlock.gif                            state[
++i] = ar[j];
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
//产生一个新的组合
InBlock.gif
                        return state;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InitCache(int length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_cachecache.ContainsKey(length))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._cache = _cachecache[length];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
this._cache = new List<int[]>();
InBlock.gif
InBlock.gif                
//first state 
InBlock.gif
                int[] state = new int[length];
InBlock.gif                
for (int i = 0; i < length; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    state[i] 
= i;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif
InBlock.gif                
while (state != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this._cache.Add(state);
InBlock.gif
InBlock.gif                    
//下一个 state
InBlock.gif
                    state = this.GetNextState(state);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                _cachecache.Add(length, 
this._cache);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
public bool MoveNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
++this._currentState;
InBlock.gif            
return this._currentState < this._cache.Count;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class ArrayEnum<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private ArrayGroupState<T> state = null;
InBlock.gif        
private T[] array = null;
InBlock.gif
InBlock.gif        
public ArrayEnum(T[] array)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.array = array;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public T[] GetGroupFirst()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            state 
= new ArrayGroupState<T>(array);
InBlock.gif
InBlock.gif            
return (T[])this.GetGroup(array, state);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public T[] GetGroupNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (state.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (T[])this.GetGroup(array, state);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private T[] GetGroup(T[] array, ArrayGroupState<T> state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            T[] ret 
= new T[array.Length];
InBlock.gif            
for (int i = 0; i < array.Length; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ret[i] 
= array[state.State[i]];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return ret;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 这里就是问题类的实现了
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Question
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public delegate bool GetResultHandler(string[] c, string[] r, string[] s, string[] p, string[] d);
InBlock.gif
InBlock.gif        
private GetResultHandler _getResultHandler = null;
InBlock.gif
InBlock.gif        
public GetResultHandler GetResult
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _getResultHandler; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _getResultHandler = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
string[] countrys,rooms,smokes,pets,drinks;
InBlock.gif        
int calcCount = 0;
InBlock.gif
InBlock.gif        
public Question()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//这里对原始数据进行输入
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.countrys =     new string[]dot.gif{"挪威"        ,"英国"            ,"瑞典"        ,"丹麦"            ,"德国"};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.rooms =         new string[]dot.gif{""        ,""            ,"绿"        ,""            ,""};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.smokes =         new string[]dot.gif{"Blends"    ,"Pall Mall"    ,"Dunhill"    ,"Blue Master"    ,"Prince"};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.pets =         new string[]dot.gif{""        ,""            ,""            ,""            ,""};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.drinks =         new string[]dot.gif{""        ,"咖啡"            ,""        ,""            ,""};
InBlock.gif
InBlock.gif            GetResult 
= new GetResultHandler(this.OnGetResult);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//这里打印第个结果 
InBlock.gif
        public virtual bool OnGetResult(string[] c, string[] r, string[] s, string[] p, string[] d)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"===================================================");
InBlock.gif
InBlock.gif            Console.WriteLine(
string.Format("country:    {0}"string.Join("\t", c)));
InBlock.gif            Console.WriteLine(
string.Format("room:       {0}"string.Join("\t", r)));
InBlock.gif            Console.WriteLine(
string.Format("smoke:      {0}"string.Join("\t", s)));
InBlock.gif            Console.WriteLine(
string.Format("pet:        {0}"string.Join("\t", p)));
InBlock.gif            Console.WriteLine(
string.Format("drink:      {0}"string.Join("\t", d)));
InBlock.gif
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
ExpandedSubBlockEnd.gif       
*/

InBlock.gif
InBlock.gif        
//这里判断是不是正确的解
InBlock.gif
        private bool isOK(string[] c,string[] r,string[] s,string[] p,string[] d)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//1、英国人住红色房子 
InBlock.gif
            if((c != null && r != null&& (Array.IndexOf<string>(c,"英国"!= Array.IndexOf<string>(r,"")))
InBlock.gif                
return false;
InBlock.gif                
InBlock.gif            
//2、瑞典人养狗 
InBlock.gif
            if((c != null && p != null&& (Array.IndexOf<string>(c,"瑞典"!= Array.IndexOf<string>(p,"")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//3、丹麦人喝茶 
InBlock.gif
            if ((c != null && d != null&& (Array.IndexOf<string>(c, "丹麦"!= Array.IndexOf<string>(d, "")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//4、绿色房子在白色房子左面 
InBlock.gif
            if ((r != null&& (Array.IndexOf<string>(r, "绿"!= Array.IndexOf<string>(r, ""- 1))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif             
//5、绿色房子主人喝咖啡 
InBlock.gif
            if ((r != null && d != null&& (Array.IndexOf<string>(r, "绿"!= Array.IndexOf<string>(d, "咖啡")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif             
//6、抽Pall Mall 香烟的人养鸟 
InBlock.gif
            if ((s != null && p != null&& (Array.IndexOf<string>(s, "Pall Mall"!= Array.IndexOf<string>(p, "")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//7、黄色房子主人抽Dunhill 香烟 
InBlock.gif
            if ((r != null && s != null&& (Array.IndexOf<string>(r, ""!= Array.IndexOf<string>(s, "Dunhill")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//8、住在中间房子的人喝牛奶 
InBlock.gif
            if ((d != null&& (Array.IndexOf<string>(d, ""!= d.Length / 2))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//9、 挪威人住第一间房 
InBlock.gif
            if ((c != null&& ((Array.IndexOf<string>(c, "挪威"!= 0&& (Array.IndexOf<string>(c, "挪威"!= c.Length - 1)))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//10、抽Blends香烟的人住在养猫的人隔壁 
InBlock.gif
            if ((s != null && p != null&& (Math.Abs(Array.IndexOf<string>(s, "Blends"- Array.IndexOf<string>(p, "")) != 1))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//11、养马的人住抽Dunhill 香烟的人隔壁 
InBlock.gif
            if ((s != null && p != null&& (Math.Abs(Array.IndexOf<string>(s, "Dunhill"- Array.IndexOf<string>(p, "")) != 1))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//12、抽Blue Master的人喝啤酒 
InBlock.gif
            if ((s != null && d != null&& (Array.IndexOf<string>(s, "Blue Master"!= Array.IndexOf<string>(d, "")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//13、德国人抽Prince香烟 
InBlock.gif
            if ((c != null && s != null&& (Array.IndexOf<string>(c, "德国"!= Array.IndexOf<string>(s, "Prince")))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//14、挪威人住蓝色房子隔壁 
InBlock.gif
            if ((c != null && r != null&& (Math.Abs(Array.IndexOf<string>(c, "挪威"- Array.IndexOf<string>(r, "")) != 1))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
//15、抽Blends香烟的人有一个喝水的邻居
InBlock.gif
            if ((s != null && d != null&& (Math.Abs(Array.IndexOf<string>(s, "Blends"- Array.IndexOf<string>(d, "")) != 1))
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//这里产生可能的结果 
InBlock.gif
        public void CalcResult()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            ArrayEnum
<string> ci = new ArrayEnum<string>(this.countrys);
InBlock.gif            ArrayEnum
<string> ri = new ArrayEnum<string>(this.rooms);
InBlock.gif            ArrayEnum
<string> si = new ArrayEnum<string>(this.smokes);
InBlock.gif            ArrayEnum
<string> pi = new ArrayEnum<string>(this.pets);
InBlock.gif            ArrayEnum
<string> di = new ArrayEnum<string>(this.drinks);
InBlock.gif
InBlock.gif
InBlock.gif            
this.calcCount = 0;
InBlock.gif            
//通过对数组进行排序来计算
InBlock.gif
            for(string[] c = ci.GetGroupFirst(); c != null; c = ci.GetGroupNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(!this.isOK(c,null,null,null,null))
InBlock.gif                    
continue;
InBlock.gif                    
InBlock.gif                
for(string[] r = ri.GetGroupFirst(); r != null; r = ri.GetGroupNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(!this.isOK(c,r,null,null,null))
InBlock.gif                        
continue;
InBlock.gif                    
InBlock.gif                    
for(string[] s = si.GetGroupFirst(); s != null; s = si.GetGroupNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if(!this.isOK(c,r,s,null,null))
InBlock.gif                            
continue;
InBlock.gif                    
InBlock.gif                        
for(string[] p = pi.GetGroupFirst(); p != null ; p = pi.GetGroupNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if(!isOK(c,r,s,p,null))
InBlock.gif                                
continue;
InBlock.gif                    
InBlock.gif                            
for(string[] d = di.GetGroupFirst(); d != null ; d = di.GetGroupNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
//如果结果正确
InBlock.gif
                                if (isOK(c, r, s, p, d))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    
//打印出来结果
InBlock.gif
                                    if (this.GetResult(c, r, s, p, d))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
dot.gif{
InBlock.gif                                        
return;
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockEnd.gif                                }

InBlock.gif                                    
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif                        
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值