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;
}

最新文章

  1. 理解Docker(6):若干企业生产环境中的容器网络方案
  2. 第11章 Java异常与异常处理
  3. WPF整理-XAML构建后台类对象
  4. Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏
  5. new(C# 参考)
  6. 活跃天数计算用户等级模仿QQ的升级方式
  7. 使用ServerSocket创建TCP服务器端
  8. Java SSH远程执行Shell脚本实现(转)
  9. Python-Networkx
  10. DelphiXE7操作sqlite数据库
  11. java.util.Dictionary源码分析
  12. CSharp - Comparison between IComparer and IComparable
  13. Node.js Express框架
  14. C# XML,XmlDocument简单操作实例
  15. C#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
  16. 在centos6,7 上编译安装内核
  17. Python3_打开和运行方式
  18. C# 获取当前屏幕DPI
  19. adb.exe 已停止工作 解决
  20. 初步了解Bootstrap4

热门文章

  1. 【读书笔记】Linux命令行与Shell脚本编程大全
  2. Python3+pygame中国象棋 代码完整 非常好 有效果演示
  3. MySQL基础知识:启动管理和账号管理
  4. 打造综合性智慧城市之朔州开发区 3D 可视化
  5. 面试必备——Java多线程与并发(二)
  6. slickgrid ( nsunleo-slickgrid ) 1 开篇有益
  7. 60秒定位问题,十倍程序员的Debug日常
  8. python基础学习之文件的基础操作方法
  9. P1328_生活大爆炸版石头剪刀布(JAVA语言)
  10. 攻防世界 reverse seven