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-08-04 16:06:11
 3 Problem: PAT_A1148#Werewolf - Simple Version
 4 AC: 16:40
 5 
 6 题目大意:
 7 狼人杀,N名玩家互爆对方身份;
 8 一共有两只狼,一共两人撒谎,
 9 狼中有一人撒谎,民中有一人撒谎
10 找出两只狼;
11 
12 基本思路:
13 id[i]表示身份;
14 liar[i]表示i号玩家是否说谎,cnt统计说谎人数;
15 假设i,j是狼人,遍历各个玩家发言
16 liar[i]+liar[j]==1且cnt==2时,符合要求,退出循环
17 */
18 #include<cstdio>
19 #include<algorithm>
20 #include<cmath>
21 using namespace std;
22 const int M=1e3;
23 
24 int main()
25 {
26 #ifdef ONLINE_JUDGE
27 #else
28     freopen("Test.txt", "r", stdin);
29 #endif // ONLINE_JUDGE
30 
31     int n,spk[M];
32     scanf("%d", &n);
33     for(int i=1; i<=n; i++)
34         scanf("%d", &spk[i]);
35     for(int i=1; i<n; i++)
36     {
37         for(int j=i+1; j<=n; j++)
38         {
39             int liar[M]={0},id[M],cnt=0;
40             fill(id+1,id+n+1,1);
41             id[i]=id[j]=-1;
42             for(int k=1; k<=n; k++)
43             {
44                 if(spk[k]*id[abs(spk[k])]<0)
45                 {
46                     liar[k]=1;
47                     cnt++;
48                 }
49             }
50             if(liar[i]+liar[j]==1 && cnt==2)
51             {
52                 printf("%d %d", i,j);
53                 n=0;
54             }
55         }
56     }
57     if(n)
58         printf("No Solution");
59 
60     return 0;
61 }

 

posted @ 2019-05-16 20:52  林東雨  阅读(393)  评论(0编辑  收藏  举报