UVa 592 Island of Logic (有趣的枚举题)

592 - Island of Logic

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=533

The Island of Logic has three kinds of inhabitants: divine beings that always tell the truth, evil beings that always lie, and human beings that are truthful during the day and lie at night. Every inhabitant recognizes the type of every other inhabitant.

A social scientist wants to visit the island. Because he is not able to distinguish the three kinds of beings only from their looks, he asks you to provide a communication analyzer that deduces facts from conversations among inhabitants. The interesting facts are whether it is day or night and what kind of beings the speakers are.

Input 

The input file contains several descriptions of conversations. Each description starts with an integer  n , the number of statements in the conversation. The following  n  lines each contain one statement by an inhabitant. Every statement line begins with the speaker's name, one of the capital letters  A B C D E , followed by a colon ` : '. Next is one of the following kinds of statements:

  • I am [not] ( divine | human | evil | lying ).
  • X is [not] ( divine | human | evil | lying ).
  • It is ( day | night ).

Square brackets [] mean that the word in the brackets may or may not appear, round brackets () mean that exactly one of the alternatives separated by | must appear. X stands for some name from ABCDE. There will be no two consecutive spaces in any statement line, and at most 50 statements in a conversation.

The input is terminated by a test case starting with n = 0.

Output 

For each conversation, first output the number of the conversation in the format shown in the sample output. Then print `` This is impossible. '', if the conversation cannot happen according to the rules or `` No facts are deducible. '', if no facts can be deduced. Otherwise print all the facts that can be deduced. Deduced facts should be printed using the following formats:

  • X is ( divine | human | evil ).
  • It is ( day | night ).

X is to be replaced by a capital letter speaker name. Facts about inhabitants must be given first (in alphabetical order), then it may be stated whether it is day or night.

The output for each conversation must be followed by a single blank line.

Sample Input 

1
A: I am divine.
1
A: I am lying.
1
A: I am evil.
3
A: B is human.
B: A is evil.
A: B is evil.
0

Sample Output 

Conversation #1
No facts are deducible.

Conversation #2
This is impossible.

Conversation #3
A is human.
It is night.

Conversation #4
A is evil.
B is divine.


Reasoning made easy 

To make things clearer, we will show the reasoning behind the third input example, where A says `` I am evil. ''. What can be deduced from this? Obviously A cannot be divine, since she would be lying, similarly A cannot be evil, since she would tell the truth. Therefore, A must be human, moreover, since she is lying, it must be night. So the correct output is as shown.

In the fourth input example, it is obvious that A is lying since her two statements are contradictory. So, B can be neither human nor evil, and consequently must be divine. B always tells the truth, thus A must be evil. Voil‘a!


思路:

1. 有多少种情况呢?

A的种族情况数*B的种族情况数*...*E的种族情况数*白天黑夜 = 3^5*2 = 486种

2. 如何枚举?

每当我们读入一句话后,就拿这句话去判断上面的486种情况哪些是谎话。(利用每个人的种族特性,注意白天夜晚)

3. 如何输出?

对于每个判断为真的情况,去计算这个情况对应的种族和白天夜晚,如果有多种情况就另作标记。


完整代码:

/*0.022s*/

#include<cstdio>
#include<cstring>
const char NAMES[3][10] = { "divine", "evil", "human" };
const int DIVINE = 0, EVIL = 1, HUMAN = 2;///神,鬼,人
const int DAY = 0, NIGHT = 1;
const int MAXPERSONS = 5, STATENUM = 486; /// 3*3*3*3*3*2(A的种族情况数*B的种族情况数*...*E的种族情况数*白天黑夜)

bool state[STATENUM];
int possible[MAXPERSONS], cas;
char s[50];

int type(int state, int p)
{
	state >>= 1;
	while (p--) state /= 3;
	return (state % 3);
}

int lying(int state, int p)///判断是否为谎话
{
	int t = type(state, p);
	return (t == EVIL || (t == HUMAN && (state & 1) == NIGHT));
}

void scanline()
{
	int i, neg, speaker, target, t = 0, flag;
	gets(s);
	speaker = s[0] - 'A';
	if (s[3] == 'I' && s[4] == 't') ///"It" is day or night.
	{
		for (i = 0; i < STATENUM; i++) ///枚举,注意每个state[i]对应一条不同的语句
		{
			flag = ((i & 1) == DAY && s[9] == 'd') ||
				   ((i & 1) == NIGHT && s[9] == 'n');
			if (lying(i, speaker) == flag) state[i] = false;///判断是否为谎话
		}
		return;
	}
	target = (s[5] == 'a' ? speaker : s[3] - 'A'); ///谈论的对象
	neg = (s[8] == 'n'); /// is "not" ?
	switch (s[8 + 4 * neg])
	{
		case 'd':
			t = DIVINE;
			break;
		case 'e':
			t = EVIL;
			break;
		case 'h':
			t = HUMAN;
			break;
		case 'l':
			for (i = 0; i < STATENUM; i++)
				if (lying(i, speaker) == (neg ^ lying(i, target))) state[i] = false;///判断是否为谎话
			return;
	}
	for (i = 0; i < STATENUM; i++)
		if (lying(i, speaker) == (neg ^ (type(i, target) == t))) state[i] = false;///判断是否为谎话
}

void output()
{
	int i, j, daynight = -1;
	bool deduction = false;
	memset(possible, -1, sizeof(possible));
	for (i = 0; i < STATENUM; i++)
		if (state[i])
		{
			if (daynight != -1 && daynight != (i & 1)) daynight = -2;///出现多种可能
			else if (daynight == -1) daynight = i & 1;
			for (j = 0; j < MAXPERSONS; j++)
			{
				if (possible[j] != -1 && possible[j] != type(i, j))///出现多种可能
					possible[j] = -2;
				else if (possible[j] == -1)
					possible[j] = type(i, j);
			}
		}
	printf("Conversation #%d\n", ++cas);
	if (daynight == -1) puts("This is impossible.");
	else
	{
		for (i = 0; i < MAXPERSONS; i++)
			if (possible[i] >= 0)
			{
				printf("%c is %s.\n", i + 'A', NAMES[possible[i]]);
				deduction = true;
			}
		if (daynight >= 0)
		{
			printf("It is %s.\n", daynight == DAY ? "day" : "night");
			deduction = true;
		}
		if (!deduction)
			puts("No facts are deducible.");
	}
	putchar(10);
}

int main()
{
	int n, i;
	while (scanf("%d\n", &n), n)
	{
		for (i = 0; i < STATENUM; i++) state[i] = true;///初始化
		for (i = 0; i < n; i++) scanline();
		output();
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值