Uva592 Island of Logic
题意:神人鬼三个种族,神只说真话,鬼只说假话,人白天说真话,晚上说假话。根据对话内容区分种族和白天黑夜。 最多有A, B, C, D, E五个人
算法:枚举A, B, C, D, E的种族情况和白天黑夜的情况。每个人有可能是神人鬼,所以用两个bit表示,白天黑夜用1个bit表示。一共需要11 bits.
#include<iostream>
#include<cstdio>
#include<string>
using namespace std; struct statement
{
char a;//speaker
char b;//object 等于状态的偏移量
bool c;//not
char d;
} p[50]; /*
A: I am [not] ( divine | human | evil | lying ).
A: X is [not] ( divine | human | evil | lying ).
A: It is ( day | night ).
*/
void get(statement &t)
{
char c;
cin >> c;
t.a = c - 'A';
cin >> c >> c;
if (c == 'I')
{
cin.get(c);
if (c == 't')//It is
t.b = 5;//day|night偏移5
else//I am
t.b = t.a;
}
else
t.b = c - 'A';//0-4对应 A-E 5对应day|night string s;
cin >> s >> s;
if (s == "not") t.c = true, cin >> s; else t.c = false;
switch (s[0])
{
case 'd': t.d = 0; break;//day divine
case 'e': t.d = 2; break;//evil
case 'l': t.d = 3; break;//lying
default: t.d = 1;//human night
}
} //human + night = 2
//evil + (day|night) >= 2
bool lying(short state, char who)
{
return (state >> who*2 & 3) + (state >> 10 & 1) >= 2;
} //this is too hard to understand
bool check(statement st, short s)
{
return (st.d == 3 ? lying(s, st.b) : st.d == (s >> st.b + st.b & 3)) ^ st.c ^ lying(s, st.a);
} //A-E 每个占2位; day,night 占1位
const int MAX_STATE=1<<11;
bool is_valid_state(int s)
{
for(int i=0;i<5;i++)
if((s>>i*2 & 3)==3)
return false;
return true;
} //this is better then check function
//判断说话的人是否说谎话
//判断话的内容是否是真的
bool judge(statement st, short s)
{
//if speaker is lying
bool speaker=lying(s, st.a); //if statement is true
bool object; //It is ( day | night ).
if(st.b==5)
{
object = (s >> 10 & 1)==st.d;
return speaker!=object;
} //lying
if(st.d==3)
{
if(st.c)
object = !lying(s, st.b);
else
object = lying(s, st.b); return speaker!=object;
} // X is [not] ( divine | human | evil )
object = (s >> st.b*2 & 3)==st.d; return speaker!=object;
} //如果状态矛盾,则置为 11b
void combine(short &a, short b)
{
for (int i = 0; i < 6; ++i)
if (a & (3 << i + i) ^ b & (3 << i + i))
a |= (3 << i + i);
} const char *s[] = { "divine", "human", "evil" }; int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva592.in", "r", stdin);
#endif
int Kase=0;
int n;
//printf("%0X\n", MAX_STATE);
while(cin>>n && n)
{
cout << "Conversation #" << ++Kase << endl;
short a = -1;
for(int i=0; i<n; i++) get(p[i]);
for(int i=0; i<MAX_STATE; i++) if(is_valid_state(i))
{
bool b=true;
for (int j=0; j<n; ++j) if (!check(p[j], i))
{
b = false;
break;
}
if (!b) continue;
if (a == -1) a = i; else combine(a, i);
}
if (a == -1) cout << "This is impossible." << endl;
if (a == 0xFFF) cout << "No facts are deducible." << endl;
for (int i = 0; i < 5; ++i)
{
int x = a>>i*2 & 3;
if (x != 3) cout << char(i + 'A') << " is " << s[x] << "." << endl;
}
int x = a>>10 & 3;
if (x != 3) cout << "It is " << (x ? "night." : "day.") << endl;
cout << endl;
} return 0;
}
Uva592 Island of Logic的更多相关文章
- uva 592 Island of Logic (收索)
Island of Logic The Island of Logic has three kinds of inhabitants: divine beings that always tel ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- QuartusII Design partion and logic lock
Design partion Design partion常用于“增益变量(QIC)”,通过Design Partition对子模块进行“逻辑分区”,在Design Partition Window中 ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- 使用struts的logic:iterate标签遍历列表时得到显示序号
<logic:notEmpty name="sList" scope="request"> <logic:iterate id="e ...
- LeetCode Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
随机推荐
- 删除特定影响因素(字段列)下的重复记录(MySQL)
;CREATE TABLE TabTest ( `id` ) NOT NULL AUTO_INCREMENT ,`factorA` ) NOT NULL DEFAULT ' ' ,`factorB` ...
- Linux 客户端 下乱码的解决方法
最近使用xshell登陆英文版redhat,由于某些文件是中文编码,在xshell下显示乱码.折腾了很久终于找到了解决的方法,希望可以对大家有用.其他语言乱码的话,解决方法和此类似! 首先检查系统的l ...
- hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...
- 把php.exe加入系统环境变量-使用命令行可快速执行PHP命令
有时候在执行长时间运行的脚本程序的时候,浏览器是架不住的.我们就可以使用CMD命令行或者LINUX命令行执行PHP程序 1.把PHP.EXE加入到环境变量,不用每次都进入到PHP的目录 ① 右击我的 ...
- 代码所见所得方式发布.xml
pre{ line-height:1; color:#b836b3; background-color:#000000; font-size:16px;}.sysFunc{color:#19ef0b; ...
- Mac OS 10.8 中的 OpenCV 开发环境设置
一.编译OpenCV 要在Mac OS上使用OpenCV,需要自己编译源代码.操作过程如下: 1)从http://www.cmake.org下载cmake 2.8安装包. 2)安装cmake 2.8. ...
- C++的类成员和类成员函数指针
类成员函数指针: 用于访问类成员函数,和一般函数指针有区别. 类成员函数处理的是类数据成员,声明类成员函数指针的同时,还要指出具体是哪个类的函数指针才可以.调用时也要通过对象调用. 而对于类的静态成员 ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- C#的默认编码
C# 的所有源代码文件,默认编码为 UTF-8,注意,是源代码文件,而不是 C# 中的 string. C# 中的所有 string,默认编码均为 Unicode (UTF-16). C# 产生的 A ...
- iOS优秀博客收录(持续更新)
唐巧 王巍 破船之家 NSHipster Limboy 无网不剩 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blo ...