PAT顶级题解目录​​​​​​​

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

题目大意:本题的简单版本在甲级中出现过,其他条件都类似,只是狼的数量和谎言家的数量不再是固定的。给出玩家数量n,狼的数量m,谎言家的数量L,现每个玩家说出一个数字,比如-2,代表该玩家认为2号是狼,若为2,则该玩家认为2号玩家是人。如果该号玩家说出的情况与实际不符合,则说明该玩家撒谎。现要找到一种满足狼的数量为m,谎言家的数量为L,且其中的狼不完全是谎言家(狼中谎言家的数量应该在[1,m)范围)。

解题思路:DFS剪枝,暴力遍搜狼,当遇到狼的数量为m的情况,则去查验该情况下谎言家的数量是否满足要求。

代码:

#include <bits/stdc++.h>
using namespace std;
int n, m, L, record[105];
bool iswolf[105] = {0}, flag = false; //分别存放该玩家是否是狼,以及是否已经找到符合条件的情况
std::vector<int> ans, tempans;
//判断谎言家是否满足要求
bool isTrue(){int cntL = 0, cntM_L = 0;for(int i = 1; i <= n; ++ i){if((iswolf[abs(record[i])] && record[i] > 0) || (!iswolf[abs(record[i])] && record[i] < 0)){++ cntL;if(iswolf[i])++ cntM_L; }}return cntL == L && cntM_L != m && cntM_L > 0;
}
void dfs(int index){if(flag)return;if(index == 0 && tempans.size() == m){if(isTrue()){flag = true;ans = tempans;}return;}if(tempans.size() > m || index == 0)return;for(int i = index; i > 0; -- i){tempans.push_back(i);iswolf[i] = 1;dfs(i-1);iswolf[i] = 0;tempans.pop_back();dfs(i-1);}
}
int main(){scanf("%d %d %d", &n, &m, &L);for(int i = 1; i <= n; ++ i)scanf("%d", &record[i]);dfs(n);if(!flag)printf("No Solution\n");else{printf("%d", ans[0]);for(int i = 1; i < ans.size(); ++ i)printf(" %d", ans[i]);}
}

1022 Werewolf (35 分)(C++)相关推荐

  1. PAT TOP 1022. Werewolf (35)

    问题描述: 1022. Werewolf (35) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Werewol ...

  2. PAT 1022. Werewolf (35)

    暴力+剪枝--我还想再优化点,试着提交一下居然过了 #include<iostream> #include<vector> #include<cmath> usin ...

  3. PAT Top 1022 Werewolf (35)

    PAT Top 1022 Werewolf (35) 题目描述 Input Specification: Output Specification: Sample Input: Sample Outp ...

  4. NBT-19年2月刊4篇35分文章聚焦宏基因组研究

    新年4篇35分文章聚焦宏基因组研究 Nature Biotechnology (NBT,自然生物技术,IF 35.7)在2019年2月刊(https://www.nature.com/nbt/volu ...

  5. NBT-新年4篇35分文章聚焦宏基因组研究

    文章目录 新年4篇35分文章聚焦宏基因组研究 1. 超高速细菌基因组检索技术 摘要 序列搜索方法 2. 宏基因组中设计全面可扩展探针捕获序列多样性 摘要 CATCH设计探针 3. 1520个人类肠道可 ...

  6. PTA 1002 Business (35分)

    想试试PTA Top Level的难度,然后随便来了一题~ 1002 Business (35分) As the manager of your company, you have to carefu ...

  7. 7-2 日期问题面向对象设计(聚合一) (35 分)

    ** 7-2 日期问题面向对象设计(聚合一) (35 分) ** 参考题目7-2的要求,设计如下几个类:DateUtil.Year.Month.Day,其中年.月.日的取值范围依然为:year∈[19 ...

  8. PAT顶级 1003 Universal Travel Sites (35分)(最大流)

    题目链接: 1003 Universal Travel Sites (35分) 思路: 题目问stationstationstation的最小容量,变相就是问最多能一次性出发多少人,使得过程中不会超过 ...

  9. PAT-Top-1003 Universal Travel Sites (35分)网络流最大流

    1003 Universal Travel Sites (35分) 题目传送门:1003 Universal Travel Sites (35分) 一.题目大意 二.解题思路 网络流问题,第一次尝试, ...

最新文章

  1. MBProgressHUD 使用详解
  2. Pat乙级 1045 快速排序
  3. 如何理解Library List
  4. java 自旋锁_java锁的种类以及辨析(一):自旋锁
  5. 五大板块(4)——链表
  6. elementUI + vue 地址跳转:无法返回上一页 - 解决篇
  7. spark学习-38-Spark的MemoryManager
  8. 【jQuery笔记Part4】02-jQuery微博案例
  9. JAVA 基础(0)教学视频的选择和笔记本的选择
  10. 前端实现PDF文件下载的两种方式
  11. Java全栈工程实践
  12. DTU服务器接收软件
  13. 专用计算机房属于中危险等级,普通住宅属哪种危险等级的灭火器配置场所
  14. linux pvs命令安装,使用linux的pvs命令格式化输出物理卷信息报表
  15. L W V th t发音
  16. 谈个人网站发展及赚钱
  17. 1、大道至简的数据处理工具-(Microsoft Power Query入门)
  18. 12、【李宏毅机器学习(2017)】Semi-supervised Learning(半监督学习)
  19. 【NVIDIA】 CUDA Toolkit工具包下载
  20. 9月第4周要闻回顾:云安全奇虎啸金山 不开源Novell忙赚钱

热门文章

  1. 关于注册公司流程(实操)。
  2. 数据结构视频教程 -《数据结构深度实战专题班 C语言版(国嵌 唐老师主讲)(非常犀利)》
  3. MATLAB中 下划线的显示问题
  4. IDEA快捷键(待重新整理,重复的有点多)
  5. 想请教下墓碑机制相关?
  6. 淘宝店铺运营技巧,宝贝转化率的因素有哪些,如何提高店铺转化
  7. linux+锐龙+段错误,锐龙三代BIOS更新出现BUG,不少用户已中招
  8. java读取PDF页数
  9. python中rand和randn_基于numpy.random.randn()与rand()的区别详解
  10. 外网访问群晖NAS(神卓NAS公网助手的安装记录)