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

题意:

  有N个人,这里面有两只是狼人,一只狼人说真话,另一只狼人说假话,另外还有一个人类说假话,判断那两个人是狼人。

思路:

  看大佬写的吧~

Code:

#include<iostream>
#include<vector>
#include<cmath>
#include<set>
#include<algorithm> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) {
return a.second < b.second;
} else {
return a.first < b.first;
}
} int main() {
int n, t;
cin >> n; set<int> human;
set<int> werewolf;
vector<int> said(n+1, 0);
vector<pair<int, int> > twoWereWolf; for (int i = 1; i <= n; ++i) {
cin >> t;
said[i] = t;
} bool wrong = false;
int werewolf1, werewolf2;
bool foundWerewolf1 = false;
bool foundWerewolf2 = false;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (j == i) continue;
werewolf1 = -1;
werewolf2 = -1;
wrong = false;
foundWerewolf1 = false;
foundWerewolf2 = false;
human.clear();
werewolf.clear();
for (int k = 1; k <= n; ++k) {
if (k != i && k != j) {
if (said[k] < 0)
werewolf.insert(abs(said[k]));
else
human.insert(said[k]);
}
}
for (int h : human) {
if (h == i) {
werewolf1 = j;
foundWerewolf1 = true;
}
if (h == j) {
werewolf1 = i;
foundWerewolf1 = true;
}
}
for (int w : werewolf) {
if (w != werewolf1) {
werewolf2 = w;
foundWerewolf2 = true;
}
}
if (foundWerewolf1 && foundWerewolf2) {
if (human.find(foundWerewolf1) == human.end() && human.find(foundWerewolf2) == human.end())
if (werewolf.find(werewolf1) != werewolf.end() || werewolf.find(werewolf2) != werewolf.end())
twoWereWolf.push_back({werewolf1, werewolf2});
}
}
} sort(twoWereWolf.begin(), twoWereWolf.end(), cmp); if (twoWereWolf.size() > 0) {
cout << twoWereWolf[0].first << " " << twoWereWolf[0].second << endl;
} else {
cout << "No Solution" << endl;
} return 0;
}

记得上上次考试的时候我没有做出来这道题,今天又做了一下,只通过了两组样例。


大佬的代码:https://www.liuchuo.net/archives/6494

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n+1);
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
vector<int> lie, a(n + 1, 1);
a[i] = a[j] = -1;
for (int k = 1; k <= n; k++)
if (v[k] * a[abs(v[k])] < 0) lie.push_back(k);
if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) {
cout << i << " " << j;
return 0;
}
}
}
cout << "No Solution";
return 0;
}

1148 Werewolf - Simple Version的更多相关文章

  1. PAT 1148 Werewolf - Simple Version

    1148 Werewolf - Simple Version (20 分)   Werewolf(狼人杀) is a game in which the players are partitioned ...

  2. PAT 1148 Werewolf - Simple Version [难理解]

    1148 Werewolf - Simple Version (20 分) Werewolf(狼人杀) is a game in which the players are partitioned i ...

  3. PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理

    题目链接:1148 Werewolf - Simple Version (20 point(s)) Description Werewolf(狼人杀) is a game in which the p ...

  4. 1148 Werewolf - Simple Version (20 分)

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  5. PAT_A1148#Werewolf - Simple Version

    Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...

  6. PAT A1148 Werewolf - Simple Version (20 分)——暴力遍历,负负得正

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  7. [Functional Programming] Write a simple version of Maybe

    Maybe has two types: Just / Nothing. Just() will just return the value that passed in. Nothing retur ...

  8. PAT 2018 秋

    A 1148 Werewolf - Simple Version 思路比较直接:模拟就行.因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟. #include <cstdio> # ...

  9. 2018.9.8pat秋季甲级考试

    第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程 ...

随机推荐

  1. MySql_176. 第二高的薪水 + limit + distinct + null

    MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...

  2. HDOJ-6628(dfs+第k字典序最小差异序列)

    permutation 1 HDOJ-6628 这题使用的暴力深搜,在dfs里面直接从最小的差异开始枚举 注意这里的pre记录前一个数,并且最后答案需要减去排列中最小的数再加一 这里有一个技巧关于求第 ...

  3. 在windows 下查看ip 地址和 在ubundu 下查看IP地址

    在windows 下查看ip 地址和 在ubundu 下查看IP地址 1.在windows 下查看 IP地址:ipconfig 2.在 ubundu 下查看IP地址:ifconfig

  4. 从零搭建一个IdentityServer——单页应用身份验证

    上一篇文章我们介绍了Asp.net core中身份验证的相关内容,并通过下图描述了身份验证及授权的流程: 注:改流程图进行过修改,第三方用户名密码登陆后并不是直接获得code/id_token/acc ...

  5. List调用toString()方法后,去除两头的中括号

    import org.apache.commons.lang.StringUtils; public class Test {    public static void main(String[] ...

  6. 扫盲贴|如何评价一款App的稳定性和质量?

    作者:友盟+移动开发专家 张文 「崩溃」与「卡顿」.「异常退出」等一样,是影响App稳定性常见的三种情况.相关数据显示,当iOS的崩溃率超过0.8%,Android的崩溃率超过0.4%的时候,活跃用户 ...

  7. P1092 虫食算 题解(搜索)

    题目链接 P1092 虫食算 解题思路 好题啊!这个搜索好难写...... 大概是要考虑进位和考虑使用过某个数字这两个东西,但就很容易出错...... 首先这个从后往前搜比较好想,按照从后往前出现的顺 ...

  8. POJ_2533 Longest Ordered Subsequence 【LIS】

    一.题目 Longest Ordered Subsequence 二.分析 动态规划里的经典问题.重在DP思维. 如果用最原始的DP思想做,状态转移方程为$DP[i] = max(DP[j] + 1) ...

  9. API管理工具

    开源的api文档管理系统 api文档 php 在项目中,需要协同开发,所以会写许多API文档给其他同事,以前都是写一个简单的TXT文本或Word文档,口口相传,这种方式比较老土了,所以,需要有个api ...

  10. juc下Condition类解析

    在使用Lock之前,我们使用的最多的同步方式应该是synchronized关键字来实现同步方式了.配合Object的wait().notify()系列方法可以实现等待/通知模式. Condition接 ...