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 [(i≤k) 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 }

原文地址:https://www.cnblogs.com/blue-lin/p/10940560.html

时间: 2024-10-01 04:50:44

PAT_A1148#Werewolf - Simple Version的相关文章

1148 Werewolf - Simple Version (20 分)

1148 Werewolf - Simple Version (20 分) 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: "P

1148 Werewolf - Simple Version (20 分)

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

1148 Werewolf - Simple Version

这题我觉得真的有点烦,我想了半天还是没全对...还是参考大神的... 1 #include <iostream> 2 #include <stdlib.h> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<map> 8 #include<set> 9 #include <un

2018.9.8pat秋季甲级考试

第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程序考场的vs2013不能粘贴,还得一点点输上去.浪费了很多时间. 1.Werewolf - Simple Version 之前遇到这种题目较少,所以刚开始没什么思路.后来想到的方法是假设撒谎的两个人分别是i.j,然后遍历所有i.j,找出符合条件的.当时总共用了得有一个小时,但程序一直得不到正确答案,

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

PAT 2018 秋

A 1148 Werewolf - Simple Version 思路比较直接:模拟就行.因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟. 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 7 using namespace std; 8 int N; 9 vector&l

Matlab学习(可以用MATLAB作曲)

从网上复制了一份matlab演奏的曲调源码,很有意思!(也很好听!) (源码来源:https://www.zhihu.com/question/27850677/answer/104060849) 1.因为复制问题,使用python简单处理了下: #!/usr/bin/python# -*- coding: UTF-8 -*- def writefile(sentences): fo = open("song.m", "w") for index, item in

hdoj 1787 GCD Again【欧拉函数】

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2673    Accepted Submission(s): 1123 Problem Description Do you have spent some time to think and try to solve those unsolved problem af

netty Getting Started--reference

reference from:http://docs.jboss.org/netty/3.1/guide/html/start.html 1.1. Before Getting Started 1.2. Writing a Discard Server 1.3. Looking into the Received Data 1.4. Writing an Echo Server 1.5. Writing a Time Server 1.6. Writing a Time Client 1.7.