PAT_A1148#Werewolf - Simple Version

Posted blue-lin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT_A1148#Werewolf - Simple Version相关的知识,希望对你有一定的参考价值。

Source:

PAT A1148 Werewolf - Simple Version (20 分)

Description:

Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game,

  • player #1 said: "Player #2 is a werewolf.";
  • player #2 said: "Player #3 is a human.";
  • player #3 said: "Player #4 is a werewolf.";
  • player #4 said: "Player #5 is a human."; and
  • player #5 said: "Player #4 is a human.".

Given that there were 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. Can you point out the werewolves?

Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. You are supposed to point out the werewolves.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (5). Then N lines follow and the i-th line gives the statement of the i-th player (1), which is represented by the index of the player with a positive sign for a human and a negative sign for a werewolf.

Output Specification:

If a solution exists, print in a line in ascending order the indices of the two werewolves. The numbers must be separated by exactly one space with no extra spaces at the beginning or the end of the line. If there are more than one solution, you must output the smallest solution sequence -- that is, for two sequences [ and [, if there exists 0 such that [(ik) and [, then A is said to be smaller than B. In case there is no solution, simply print No Solution.

Sample Input 1:

5
-2
+3
-4
+5
+4

Sample Output 1:

1 4

Sample Input 2:

6
+6
+3
+1
-5
-2
+4

Sample Output 2 (the solution is not unique):

1 5

Sample Input 3:

5
-2
-3
-4
-5
-1

Sample Output 3:

No Solution

Keys:

  • 简单模拟

Attention:

  • 穷举即可

Code:

 1 /*
 2 Data: 2019-05-28 21:56:56
 3 Problem: PAT_A1148#Werewolf - Simple Version
 4 AC: 25:00
 5 
 6 题目大意:
 7 游戏中有两个狼人和若干平民,依次指认狼人或平民,狼和民各一人说谎
 8 根据发言找出两个狼人
 9 输入:
10 第一行,人数5<=N<=100;
11 接下来N行,表示1<=i<=N号玩家的发言,正数指认为民,负数指认为狼
12 输出:
13 从小到大输出两个狼人玩家序号,结果不唯一则输出较小序列
14 */
15 #include<cstdio>
16 #include<algorithm>
17 #include<cmath>
18 using namespace std;
19 const int M=110;
20 int say[M],wolf[M],liar[M];
21 
22 int main()
23 {
24 #ifdef ONLINE_JUDGE
25 #else
26     freopen("Test.txt", "r", stdin);
27 #endif // ONLINE_JUDGE
28 
29     int n;
30     scanf("%d", &n);
31     for(int i=1; i<=n; i++)
32         scanf("%d", &say[i]);
33     for(int i=1; i<=n; i++)
34     {
35         for(int j=i+1; j<=n; j++)
36         {
37             fill(liar,liar+M,0);
38             fill(wolf,wolf+M,1);
39             wolf[i]=-1;wolf[j]=-1;
40             int cnt=0;
41             for(int k=1; k<=n; k++)
42             {
43                 if(say[k]*wolf[abs(say[k])]<0){
44                     cnt++;liar[k]=1;
45                 }
46                 else
47                     wolf[abs(say[k])]=say[k];
48             }
49             if(cnt==2 && liar[i]+liar[j]==1){
50                 printf("%d %d\n", i,j);
51                 return 0;
52             }
53         }
54     }
55     printf("No Solution\n");
56 
57     return 0;
58 }

 

以上是关于PAT_A1148#Werewolf - Simple Version的主要内容,如果未能解决你的问题,请参考以下文章

1148 Werewolf - Simple Version (20分)

1148 Werewolf - Simple Version (20 分)

1148 Werewolf - Simple Version

1148 Werewolf - Simple Version (20 分)难度: 一般 / 思维 枚举

PAT 2018 秋

2018.9.8pat秋季甲级考试