誰養貓?

來源:互聯網
上載者:User

標籤:style   color   io   os   ar   for   sp   on   c   

五個人來自不同地方,住不同房子,養不同動物,吸不同牌子香煙,喝不同飲料,喜歡不同食物。根據以下線索確定誰是養貓的人

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){//這裡一堆剪枝~(之前不加╮(╯_╰)╭,複雜度=5!^6,等//半天沒結果出來。。。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;}


誰養貓?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.