题意:神人鬼三个种族,神只说真话,鬼只说假话,人白天说真话,晚上说假话。根据对话内容区分种族和白天黑夜。  最多有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的更多相关文章

  1. uva 592 Island of Logic (收索)

      Island of Logic  The Island of Logic has three kinds of inhabitants: divine beings that always tel ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  4. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  5. QuartusII Design partion and logic lock

    Design partion Design partion常用于“增益变量(QIC)”,通过Design Partition对子模块进行“逻辑分区”,在Design Partition Window中 ...

  6. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

  7. Leetcode-463 Island Perimeter

    #463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...

  8. 使用struts的logic:iterate标签遍历列表时得到显示序号

    <logic:notEmpty name="sList" scope="request"> <logic:iterate id="e ...

  9. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

随机推荐

  1. 使用Jquery Mobile设计Android通讯录

    本系列教程将指导大家一步步使用Jquery Mobile设计一个Android的通讯录应用.其中在应用的界面部分,将使用jQuery Mobile框架,并且会指导大家如何使Android中提供的web ...

  2. 嵌入式 fork与vfork的区别

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1.  fork  ():子进程拷贝父进程的数据段,代码段     vfork ( ):子进程与父进程共享数据段 ...

  3. linux静态与动态库创建及使用实例

    一,gcc基础语法: 基本语法结构:(由以下四部分组成) gcc -o 可执行文件名 依赖文件集(*.c/*.o) 依赖库文件及其头文件集(由-I或-L与-l指明) gcc 依赖文件集(*.c/*.o ...

  4. Sikulix IDE简介

    打开sikuklixIDE 这里介绍下使用方式 可以看到左边的menu 有查找,鼠标动作和键盘动作 我们先用百度搜索做个例子 打开firefox,输入www.baidu.com 点击左边的Click, ...

  5. Initializing a Build Environment

    This section describes how to set up your local work environment to build the Android source files. ...

  6. NewtonPrincipia_物体的运动_求向心力

    NewtonPrincipia_物体的运动_求向心力 让我们看一下十七世纪的被苹果砸中的艾萨克,是怎样推导出向心力公式的 在现在的观点看来,其中涉及到的很多没有符号表示的微分量.下面的内容只是叙述了推 ...

  7. Linux Shell脚本教程

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  8. Windows Azure 虚拟网络配置(Point to Site)

    说明:本文以Azure国际版为例,中国版在网络位置会存在一定差异. 1. 场景 虚拟网络为我们提供了在Windows Azure云计算环境上构建网络定义的能力,通过虚拟网络,我们可以方便地将Windo ...

  9. Chapter 6 Windows下编译pycaffe

    pycaffe 是 Caffe 提供的Python接口,我们知道 Caffe 的整个framework是基于C Plus Plus 来开发的,所以pycaffe 的根本就是提供Python调用Caff ...

  10. 怎么利用C#中的 webclient 创建cookie

    Cookies are not limited only to web browsers. any http-aware client that supports cookies can deal w ...