PAT_A1148#Werewolf - Simple Version

Source:

PAT 1148 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:

  • 基本思路就是穷举,一种方法是找liar,另一种是找wolf,第一种比较直观,第二种更快一些;

Code:

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

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/10877910.html