题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=533
人数最多只有5个人,加上时间,所以可以用6个循环枚举所有的情况。
输入的时候可以根据某个特定的字符去判断这个陈述的内容,比如带有'd'的是'divine'。
判断该陈述是否合法还要结合说话人的身份去判断。如果所有的陈述都合法,那么枚举的这种情况是可能的,把它保存下来,枚举完所以情况后再进行判断。
人数最多只有5个人,加上时间,所以可以用6个循环枚举所有的情况。
输入的时候可以根据某个特定的字符去判断这个陈述的内容,比如带有'd'的是'divine'。
判断该陈述是否合法还要结合说话人的身份去判断。如果所有的陈述都合法,那么枚举的这种情况是可能的,把它保存下来,枚举完所以情况后再进行判断。
#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; struct Statement { char speaker,object,nature;//说话者、陈述对象、对象的性质 bool whether;//是否 }; Statement s[60]; int res[5][4];//保存枚举的结果 void input(int num) { string str1,str2; getline(cin,str1); for(int i=0;i<str1.length();i++)//把陈述的所有空格去掉 if(str1[i]!=' ') str2+=str1[i]; s[num].speaker=str2[0]; if(str2[3]=='t') { s[num].object='t';//陈述对象是时间 if(str2[7]=='o') { s[num].whether=false; s[num].nature=str2[9]; } else { s[num].whether=true; s[num].nature=str2[6]; } } else { if(str2[2]=='I') s[num].object=s[num].speaker; else s[num].object=str2[2]; if(str2[5]=='n') { s[num].whether=false; s[num].nature=str2[8]; } else { s[num].whether=true; s[num].nature=str2[5]; } } } bool judge(int _fact,int t,bool whether,char statement)//判断陈述的性质与事实是否相符 { char fact; if(_fact==1) fact='d'; else if(_fact==2) fact='e'; else fact='h'; if(statement=='l'&&whether) { if(fact=='e'||fact=='h'&&t==2)//如果指证对方说谎,那么对方必须是恶魔或者晚上的人类才陈述正确 return 1; else return 0; } if(statement=='l'&&!whether)//如果澄清对方没有说谎,那么对方必须是牧师或者白天的人类才陈述正确 { if(fact=='d'||fact=='h'&&t==1) return 1; else return 0; } if((statement==fact)==whether)//同为真或同为假时陈述正确 return 1; else return 0; } bool islegal(int num,int A,int B,int C,int D,int E,int t) { bool stateistrue;//记录陈述是否正确 if(s[num].object=='t')//如果陈述的是时间 { char time; if(t==1) time='d'; else time='n'; if((s[num].nature==time)==(s[num].whether))//同为真或同为假时陈述正确 stateistrue=1; else stateistrue=0; } else { switch(s[num].object) { case'A':stateistrue=judge(A,t,s[num].whether,s[num].nature);break; case'B':stateistrue=judge(B,t,s[num].whether,s[num].nature);break; case'C':stateistrue=judge(C,t,s[num].whether,s[num].nature);break; case'D':stateistrue=judge(D,t,s[num].whether,s[num].nature);break; case'E':stateistrue=judge(E,t,s[num].whether,s[num].nature);break; } } if(stateistrue)//判断完陈述是否正确还需要结和说话人的身份去确认陈述是否合法 { switch(s[num].speaker) { case'A':if(A==1||A==3&&t==1) return 1;else return 0; case'B':if(B==1||B==3&&t==1) return 1;else return 0; case'C':if(C==1||C==3&&t==1) return 1;else return 0; case'D':if(D==1||D==3&&t==1) return 1;else return 0; case'E':if(E==1||E==3&&t==1) return 1;else return 0; } } else { switch(s[num].speaker) { case'A':if(A==2||A==3&&t==2) return 1;else return 0; case'B':if(B==2||B==3&&t==2) return 1;else return 0; case'C':if(C==2||C==3&&t==2) return 1;else return 0; case'D':if(D==2||D==3&&t==2) return 1;else return 0; case'E':if(E==2||E==3&&t==2) return 1;else return 0; } } } int main() { int i,j,n,cases=0; while(cin>>n&&n) { getchar(); cases++; cout<<"Conversation #"<<cases<<endl; for(i=0;i<n;i++) { input(i);//输入第i个陈述 } memset(res,0,sizeof(res)); int day=0,night=0; for(int A=1;A<=3;A++)//1、2、3分别代表divine,evil,human for(int B=1;B<=3;B++) for(int C=1;C<=3;C++) for(int D=1;D<=3;D++) for(int E=1;E<=3;E++) for(int t=1;t<=2;t++)//1表示白天,2表示夜晚 { int ok=1; for(i=0;i<n;i++) if(islegal(i,A,B,C,D,E,t)){}//判断该陈述是否合法 else { ok=0; break; } if(ok) { res[0][A]=1; res[1][B]=1; res[2][C]=1; res[3][D]=1; res[4][E]=1; if(t==1) day=1; else night=1; } } int impossible=0,nodeducible=1; for(i=0;i<5;i++) { int cnt=0,index; for(j=1;j<=3;j++) if(res[i][j]) { cnt++; index=j; } if(cnt==0) { impossible=1; break; } else if(cnt==1) { char person=i+'A'; if(index==1) cout<<person<<" is divine."<<endl; else if(index==2) cout<<person<<" is evil."<<endl; else cout<<person<<" is human."<<endl; nodeducible=0; } } if(!day&&!night) impossible=1; else if(day&&!night) { cout<<"It is day."<<endl; nodeducible=0; } else if(night&&!day) { cout<<"It is night."<<endl; nodeducible=0; } if(impossible) cout<<"This is impossible."<<endl; else if(nodeducible) cout<<"No facts are deducible."<<endl; putchar('\n'); } return 0; }
发表评论
-
UVa 10422 Knights in FEN
2012-09-07 08:40 901题目:http://uva.onlinejudge.org/i ... -
UVa 539 The Settlers of Catan
2012-08-31 22:22 28题目:http://uva.onlinejudge.org/i ... -
UVa 301 Transportation
2012-08-31 22:10 34题目:http://uva.onlinejudge.org/i ... -
UVa 639 Don't Get Rooked
2012-08-30 23:01 811题目:http://uva.onlinejudge.org/i ... -
UVa 216 Getting in Line
2012-08-29 20:48 723题目:http://uva.onlinejudge.org/i ... -
UVa 10474 Where is the Marble?
2012-08-28 13:45 851题目:http://uva.onlinejudge.org/i ... -
UVa 11205 The broken pedometer
2012-08-25 17:28 1049题目:http://uva.onlinejudge.org/i ... -
UVa 131 The Psychic Poker Player
2012-08-24 22:28 873题目:http://uva.onlinejudge.org/i ... -
UVa 729 The Hamming Distance Problem
2012-08-24 12:18 695题目:http://uva.onlinejudge.org/i ... -
Uva 10098 Generating Fast
2012-08-23 15:28 659题目:http://uva.onlinejudge.org/i ... -
UVa 146 ID Codes
2012-08-20 18:46 763题目:http://uva.onlinejudge.org/i ... -
UVa 10167 Birthday Cake
2012-08-16 20:57 604题目:http://uva.onlinejudge.org/i ... -
UVa 10129 Play on Words
2012-08-15 22:49 1126题目:http://uva.onlinejudge.org/i ... -
UVa 10596 Morning Walk
2012-08-14 22:05 879题目:http://uva.onlinejudge.org/i ... -
Uva 10305 Ordering Tasks
2012-08-13 23:40 657题目:http://uva.onlinejudge.org/i ... -
Uva 10004 Bicoloring
2012-08-13 23:34 873题目:http://uva.onlinejudge.org/i ... -
Uva 532 Dungeon Master
2012-08-13 23:29 787题目:http://uva.onlinejudge ... -
Uva 439 Knight Moves
2012-08-11 22:24 656题目:http://uva.onlinejudge.org/i ... -
UVa 784 Maze Exploration
2012-08-11 14:09 824题目:http://uva.onlinejudge.org/i ... -
Uva 572 Oil Deposits
2012-08-11 11:43 745题目:http://uva.onlinejudge.org/i ...
相关推荐
UVA109的题解,经测试完全正确,还附有题解。
uva272
有uva刘汝佳文件夹的50道题解,从数据结构开始,以后慢慢上传
包含UVA在线OJ系统的绝大部分的示例代码,并都已AC,可在刷题时参考
UVa在我看来是比较全的一个题解,希望能帮助大家。欢迎下载。
uva最全ac代码
uva531最长公共子序列问题水题,应用简单的dp即可ac有更快速的方法欢迎讨论
uva10755 ac 代码,可以随意更改下载
uva357的栈实现版本
UVA 题目,不是很难,试试吧
《算法竞赛入门经典》UVa配套题目pdf版完整
1.Uva_base的编译 在编译球队时,则需要在当前球队文件夹下打开终端输入执行以下命令(以下命令都是在root下执行的): ./configure make clean make 如果运行Uva_base后,出现球员越界或掉线的情况,就重新...
世界著名大学UVA OJ平台上的题目部分分类,分的不好请原谅。
这是一支完整的uva球队,包含所有基本模块,初者可在上修改得到自己的球队
uva_trilearn2002 源代码
主要是uvaoj习题相关题目 练习题目
这里面全部为在Uva Online Judge上面的部分题目的解答,里面提供了解答使用的源代码。
PDF试题
开源项目-codingsince1985-UVa.zip,Been solving UVa Online Judge Problems in Golang for one year (and counting)
UVA 499 Solution in C/ C++