PTA Werewolf

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 liers. Can you point out the werewolves?

Now you are asked to solve a harder vertion of this problem: given that there were N players, with M werewolves among them, at least one but not all the werewolves were lying, and there were exactly L liers. You are supposed to point out the werewolves.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integer N (5 ≤ N ≤ 100), M and L (2 ≤ M,L < N). Then N lines follow and the i-th line gives the statement of the i-th player (1 ≤ i ≤ N), 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 descending order the indices of the M 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 largest solution sequence – that is, for two sequences A = { a[1], …, a[M] } and B = { b[1], …, b[M] }, if there exists 0 ≤ k < M such that a[i] = b[i] (i ≤ k) and a[k+1]>b[k+1], then A is said to be larger than B. In case there is no solution, simply print No Solution.

Sample Input 1:

5 2 2
-2
+3
-4
+5
+4

Sample Output 1:

4 1

Sample Input 2:

6 2 3
-2
+3
-4
+5
+4
-3

Sample Output 2:

6 4

Sample Input 3:

6 2 5
-2
+3
-4
+5
+4
+6

Sample Output 3:

No Solution

思路:

根据题目我们不难发现,最后的输出结果是狼的标号。所以我们以找到狼作为我们的根本目标。
程序大致按照以下的思路进行:

  1. 产生n玩家,m匹狼的组合数。
  2. 验证每种情况是否符合说谎人数。一旦超过人数就立即检查下一组。
  3. 验证是否只有1m-1匹狼说谎。
  4. 输出最终解答。

代码实现:

#include<iostream>
#include<vector>
#include<algorithm>using namespace std;void backtrack(int n, int start, int m, vector<int>& path);vector<vector<int>> res; // to store the combinevector<vector<int>> combine(int n, int m) {vector<int> path;backtrack(n, n, m, path);return res;
} // compute the combinevoid backtrack(int n, int start, int m, vector<int>& path) {if (path.size() == m) {res.push_back(path);return;}for (int i = start; i >= 1; --i) {path.push_back(i);backtrack(n, i-1, m, path);path.pop_back();}
}int main() {int n, m, l; // n for the total number of players, m for wolves, and l for liarsint m_count, l_count; // temporary to count wolves and liarscin >> n >> m >> l;m_count = m;l_count = l;vector<int> ass(n+1);vector<vector<int>> result;vector<int> pre(n+1, 1);int t;for (int i = 0; i < n; i++) { // get players's assertionscin >> t;ass[i+1] = t;}combine(n, m);int count = 0;for (auto one: res) {for (auto i: one) {pre[i] = -1;}for (int i = 1; i <= n; i++) {if (ass[i] * pre[abs(ass[i])] < 0) {if (pre[i] == -1)m_count--;l_count--;}}pre.assign(n + 1, 1);if ((l_count != 0) || (m_count == 0) || (m_count == m)) {l_count = l;m_count = m;count ++;continue;} else {result.push_back(one);l_count = l;m_count = m;}}if (count != res.size()) {int s = result[0].size();sort(result[0].rbegin(), result[0].rend());for (int i = 0; i < s - 1; ++i) {cout << result[0][i] << ' ';}cout << result[0][s - 1];}else {cout << "No Solution";}
}

PTA Werewolf相关推荐

  1. 【PTA Advanced】1148 Werewolf - Simple Version(C++)

    目录 题目 Input Specification: Output Specification: Sample Input 1: Sample Output 1: Sample Input 2: Sa ...

  2. Werewolf(狼人杀)DFS与回溯算法

    回溯算法的一道例题 Werewolf(狼人杀) 题目描述: Werewolf(狼人杀) is a game in which the players are partitioned into two ...

  3. C语言 之 PTA乙级错误集锦

    1,很大很大的数输入,并各位加和  PTA-1001 #include <stdio.h> #include <math.h> int main(){int sum=0,cou ...

  4. PTA数据结构与算法题目集6-4 6-3 6-8

    PTA数据结构与算法题目集(中文) 6-4 链式表的按序号查找 ElementType FindKth( List L, int K ){int index = 0;while(L){++index; ...

  5. PTA数据结构与算法题目集 6-9 二叉树的遍历

    PTA数据结构与算法题目集(中文) 6-9 二叉树的遍历 void InorderTraversal( BinTree BT ){if(BT==NULL)return;if(BT->Left){ ...

  6. PTA 家庭房产 (图论,暴搜)

    PTA 家庭房产 (图论,暴搜) 题目详情: 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤1000),随后N ...

  7. PTA—输出全排列 (20分) 递归回溯思想

    PTA-输出全排列 (20分) 递归回溯思想 题目要求: 请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间. 输入格式: 输入给出 ...

  8. PTA 基础编程题目集 6-6 求单链表结点的阶乘和

    PTA 基础编程题目集 6-6 求单链表结点的阶乘和 本题要求实现一个函数,求单链表L结点的阶乘和.这里默认所有结点的值非负,且题目保证结果在int范围内. 函数接口定义: int Factorial ...

  9. PTA 基础编程题目集 7-27 冒泡法排序 C语言

    PTA 基础编程题目集 7-27 冒泡法排序 C语言 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们.通过一遍扫描,则最后一 ...

最新文章

  1. python3 f-strings格式字符串
  2. 012_SpringBoot视图层技术thymeleaf-条件判断
  3. NumPy入门攻略:手把手带你玩转这款强大的数据分析和计算工具
  4. TypeScript 元组(Tuple)
  5. VB.NET 反射机制取得当前函数名 类名
  6. OpenCV-Python实战(番外篇)——OpenCV、NumPy和Matplotlib直方图比较
  7. java初学者笔记总结day7
  8. rpm安装mysql指定数据仓库_linux(center OS7)安装JDK、tomcat、mysql 搭建java web项目运行环境-Go语言中文社区...
  9. JQuery模拟MAC任务栏放大效果
  10. 网站出现502 BAD GATEWAY的解决办法
  11. 攻防世界-mfw-(详细操作)做题笔记
  12. Android 11.0 MTK去掉开机通过长按电源键+音量加进入recovery 工厂模式
  13. bitwise ssh client的使用
  14. 网卡驱动修改服务器,网卡驱动配置
  15. Python使用cairosvg将SVG转PNG设置dpi无效
  16. html页面实现打印
  17. 工具说明书 - 英语语法检查工具Grammarly
  18. 我的markdown临时图片区(for 印象笔记web)
  19. JS和jQuery通过this获取html标签中的属性值
  20. 第一次亲密接触读后感(转)

热门文章

  1. 年终奖,明年年中发~
  2. java linkedlist用法_Java linkedList详细介绍及使用示例
  3. 天刀各大服务器位置,各司其职 天刀各门派各位置定位详解
  4. Unity自动保存系统
  5. 网易邮箱如何群发邮件
  6. html+css实现爱心制作,通俗易懂
  7. [附源码]Python计算机毕业设计茶叶产品质量安全可追溯系统
  8. .Net Core中使用NEST简单操作Elasticsearch
  9. 基于Dlib库的人脸表情分析与识别——Python
  10. java反射set_java反射