谁养猫?

来源:互联网 发布:汽车贸易额数据 编辑:程序博客网 时间:2024/04/27 18:53

五个人来自不同地方,住不同房子,养不同动物,吸不同牌子香烟,喝不同饮料,喜欢不同食物。根据以下线索确定谁是养猫的人

1,红房子在蓝房子的右边,白房子的左边(不一定紧邻)
2,黄房子的主人来自香港,而且他的房子不在最左边。
3,爱吃比萨饼的人住在爱喝矿泉水的人的隔壁。
4,来自北京的人爱喝茅台,住在来自上海的人的隔壁。
5,吸希尔顿香烟的人住在养马的人?右边隔壁。
6,爱喝啤酒的人也爱吃鸡。
7,绿房子的人养狗。
8,爱吃面条的人住在养蛇的人的隔壁。

9,来自天津的人的邻居(紧邻)一个爱吃牛肉,另一个来自成都。
10,养鱼的人住在最右边的房子里。
11,吸万宝路香烟的人住在吸希尔顿香烟的人和吸555香烟的人的中间(紧邻)
12,红房子的人爱喝茶。

13,爱喝葡萄酒的人住在爱吃豆腐的人的右边隔壁。
14,吸红塔山香烟的人既不住在吸健牌香烟的人的隔壁,也不与来自上海的人相邻。
15,来自上海的人住在左数第二间房子里
16,爱喝矿泉水的人住在最中间的房子里。
17,爱吃面条的人也爱喝葡萄酒。
18,吸555香烟的人比吸希尔顿香烟的人住的靠右。

 

敲了2hours+,这个时间我都推出来了。。。

4号房的人养猫


#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))const int MAXN=1000+10;const int INF=1<<30;using namespace std;char a[8][6];char name[][10]={"颜色","食物","地方","饮料","香烟","宠物","房号"};char Color[][10]={"","红","黄","蓝","绿","白"};char Food[][10]={"","萨饼","鸡肉","面条","牛肉","豆腐"};char From[][10]={"","北京","上海","天津","成都","香港"};char Drink[][10]={"","矿泉水","茅台","啤酒","茶","葡萄酒"};char Cigar[][10]={"","希尔顿","万宝路","555","红塔山","健牌"};char Pet[][10]={"","马","蛇","鱼","猫","狗"};char *ptr;//假设/*num   1      2      3      4      5color   红     黄     蓝     绿     白food    萨饼   鸡肉   面     牛肉   豆腐 from    北京   上海   天津   成都   香港drink   矿泉水 茅台   啤酒   茶     葡萄酒烟      希尔顿 万宝路 555    红塔山 健牌pet     马     蛇     鱼     猫     狗*/bool J1(){int red,blue,white;ptr=strchr(a[0], '1');red=ptr-a[0];ptr=strchr(a[0], '3');blue=ptr-a[0];ptr=strchr(a[0], '5');white=ptr-a[0];if(red>blue && red<white) return 1;return 0;}bool J2(){int yellow;ptr=strchr(a[0], '2');yellow=ptr-a[0];char from=a[2][yellow];if(from=='5' && yellow!=0) return 1;return 0;}bool J3(){int num1,num2;ptr=strchr(a[1], '1');num1=ptr-a[1];ptr=strchr(a[3], '1');num2=ptr-a[3];if(abs(num2-num1)==1) return 1;return 0;}bool J4(){int num1,num2;char d;ptr=strchr(a[2],'1');d=a[3][ptr-a[2]];num1=ptr-a[2];ptr=strchr(a[2],'2');num2=ptr-a[2];if(d=='2' && abs(num1-num2)==1) return 1;return 0;}bool J5(){int num1,num2;ptr=strchr(a[4],'1');num1=ptr-a[4];ptr=strchr(a[5],'1');num2=ptr-a[5];if(num1-num2==1) return 1;return 0;}bool J6(){char food;ptr=strchr(a[3],'3');food=a[1][ptr-a[3]];if(food=='2') return 1;return 0;}bool J7(){char pet;ptr=strchr(a[0],'4');pet=a[5][ptr-a[0]];if(pet=='5') return 1;return 0;}bool J8(){char pet;int num1,num2;ptr=strchr(a[1],'3');num1=ptr-a[1];ptr=strchr(a[5],'2');num2=ptr-a[5];if(abs(num1-num2)==1) return 1;return 0;}bool J9(){char food,from;int num,num1,num2;ptr=strchr(a[2],'3');num=ptr-a[2];int x=num-1;int y=num+1;if(x<0 || y>=5) return 0;if(a[1][x]=='4' && a[2][y]=='4') return 1;return 0;}bool J10(){int num;ptr=strchr(a[5],'3');num=ptr-a[5];if(num==4) return 1;return 0;}bool J11(){int num,num1,num2;ptr=strchr(a[4],'2');num=ptr-a[4];ptr=strchr(a[4],'1');num1=ptr-a[4];ptr=strchr(a[4],'3');num2=ptr-a[4];if(abs(num-num1)==1 && abs(num2-num)==1) return 1;return 0;}bool J12(){char drink;ptr=strchr(a[0],'1');drink=a[3][ptr-a[0]];if(drink=='4') return 1;return 0;}bool J13(){int num1,num2;ptr=strchr(a[3],'5');num1=ptr-a[3];ptr=strchr(a[1],'5');num2=ptr-a[1];if(num1-num2==1) return 1;return 0;}bool J14(){int num,num1,num2;ptr=strchr(a[4],'4');num=ptr-a[4];ptr=strchr(a[4],'5');num1=ptr-a[4];ptr=strchr(a[2],'2');num2=ptr-a[2];if(abs(num-num1)>1 && abs(num-num2)>1) return 1;return 0;}bool J15(){int num;ptr=strchr(a[2],'2');num=ptr-a[2];if(num==1) return 1;return 0;}bool J16(){int num;ptr=strchr(a[3],'1');num=ptr-a[3];if(num==2) return 1;return 0;}bool J17(){char drink;ptr=strchr(a[1],'3');drink=a[3][ptr-a[1]];if(drink=='5') return 1;return 0;}bool J18(){int num1,num2;ptr=strchr(a[4],'3');num1=ptr-a[4];ptr=strchr(a[4],'1');num2=ptr-a[4];if(num1-num2>0) return 1;return 0;}bool judge(){if(J1() && J2() && J3() && J4() && J5() && J6() && J7() && J8() && J9() && J10() && J11() && J12() && J13() && J14() && J15() && J16() && J17() && J18()) return 1;return 0;}void dfs(int cnt){if(cnt==1 && J1()==0) return;if(cnt==3 && J2()==0) return;if(cnt==3 && J9()==0) return;if(cnt==3 && J15()==0) return;if(cnt==4 && J3()==0) return;if(cnt==4 && J6()==0) return;if(cnt==4 && J12()==0) return;if(cnt==4 && J13()==0) return;if(cnt==4 && J16()==0) return;if(cnt==4 && J17()==0) return;if(cnt==5 && J11()==0) return;if(cnt==5 && J14()==0) return;if(cnt==5 && J18()==0) return;if(cnt==6){if(judge()){printf("\n房号:  ");for(int i=1; i<=5; i++) printf("%-10d", i);printf("\n");for(int i=0; i<6; i++){printf("%s:  ", name[i]);for(int j=0; j<5; j++){int k=a[i][j]-'0';if(i==0) printf("%-10s", Color[k]);if(i==1) printf("%-10s", Food[k]);if(i==2) printf("%-10s", From[k]);if(i==3) printf("%-10s", Drink[k]);if(i==4) printf("%-10s", Cigar[k]);if(i==5) printf("%-10s", Pet[k]);}printf("\n");}printf("\n");}return;}char b[]="12345";do{strcpy(a[cnt], b);dfs(cnt+1);}while(next_permutation(b,b+5));}int main(){//freopen("in.txt","r",stdin);dfs(0);return 0;}


0 0
原创粉丝点击