PAT TOP 1022. Werewolf (35)
问题描述:
1022. Werewolf (35)
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 (the solution is not unique):
6 4
Sample Input 3:
6 2 5 -2 +3 -4 +5 +4 +6
Sample Output 3:
No Solution
这题也是暴力dfs+剪枝算法就可以解决,只是判定条件有点复杂。。。
在dfs搜索过程中维护四个变量,分别是:(1)剩下未判定说谎与否的人数n,(2)已判定说谎的人数ln,(3)已判定是狼人人数wn,(4)未判明身份的人数un;再加上一些基本的剪枝条件,就可以AC了。。。
AC代码:
123456789 10111213141516171819 20212223242526272829 30313233343536373839 40414243444546474849 50515253545556575859 60616263646566676869 70717273747576777879 80818283848586878889 90919293949596979899 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
#include<bits/stdc++.h> using namespace std; vector<int> v,vr,vl,vi; int n,m,k; void dfs(int n,int ln,int wn,int un) {if(ln>k||wn>m||wn+un<m)return;else if(n<k-ln)return;else if(n<1){if(ln==k&&wn<=m&&wn+un>=m){int first=0;bool flag=false;int fl=0;for(int i=v.size()-1;i>0&&!flag;--i){if(vl[i]==-1&&vi[i]==-1)flag=true;}if(m-wn)for(int i=v.size()-1;i>0&&!flag;--i){if(vl[i]==-1&&vi[i]==0){fl=i;vi[i]=-1;wn++;un--;flag=true;}}if(flag){vector<int> vrr;for(int i=v.size()-1;i>0&&vrr.size()<m;--i){if(vi[i]==-1)vrr.emplace_back(i); }for(int i=v.size()-1;i>0&&vrr.size()<m;--i){if(vi[i]==0)vrr.emplace_back(i);}sort(vrr.begin(),vrr.end(),greater<int>());if(vr.empty())vr=vrr;elsefor(int i=0;i<vrr.size();++i){if(vrr[i]>vr[i]){vr=vrr;break;}}}vi[fl]=0;}}else{int vln=vl[n];int vn=abs(v[n]);int vivn=vi[vn];if(v[n]>0){if(vi[vn]<0){vl[n]=-1;dfs(n-1,ln+1,wn,un);}else if(vi[vn]>0){vl[n]=1;dfs(n-1,ln,wn,un);}else{vl[n]=1;vi[vn]=1;dfs(n-1,ln,wn,un-1);vl[n]=-1;vi[vn]=-1;dfs(n-1,ln+1,wn+1,un-1);}}else{if(vi[vn]<0){vl[n]=1;dfs(n-1,ln,wn,un);}else if(vi[vn]>0){vl[n]=-1;dfs(n-1,ln+1,wn,un);}else{vl[n]=1;vi[vn]=-1;dfs(n-1,ln,wn+1,un-1);vl[n]=-1;vi[vn]=1;dfs(n-1,ln+1,wn,un-1);} }vi[vn]=vivn;vl[n]=vln;return;} } int main() {// freopen("data.txt","r",stdin);ios::sync_with_stdio(false);int x;cin>>n>>m>>k;vl.resize(n+1,0);vi.resize(n+1,0);v.push_back(0);for(;n--;){cin>>x;v.push_back(x);}dfs(v.size()-1,0,0,v.size()-1);if(vr.empty())cout<<"No Solution";else{cout<<vr[0];for(int i=1;i<vr.size();++i)cout<<" "<<vr[i];}return 0; } |
PAT TOP 1022. Werewolf (35)相关推荐
- PAT Top 1022 Werewolf (35)
PAT Top 1022 Werewolf (35) 题目描述 Input Specification: Output Specification: Sample Input: Sample Outp ...
- pat顶级1022 Werewolf (35 point(s))
欢迎访问我的pat顶级题解目录哦 https://blog.csdn.net/richenyunqi/article/details/86751676 题目描述 算法设计 这道题是pat甲级1148 ...
- PAT 1022. Werewolf (35)
暴力+剪枝--我还想再优化点,试着提交一下居然过了 #include<iostream> #include<vector> #include<cmath> usin ...
- 1022 Werewolf (35 分)(C++)
PAT顶级题解目录 Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the ...
- PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
- PAT甲级1022 Digital Library (30分):[C++题解]结构体、排序、查询
文章目录 题目分析 题目链接 题目分析 分析: 一本书信息由6个,想到用结构体来存. 至于每一个信息可以用string来存,而关键字在想使用vector<string> keywords还 ...
- T1022 Werewolf (35 point(s))
T1022 Werewolf (35 point(s)) 文章目录 T1022 Werewolf (35 point(s)) 题干 Input Specification: Output Specif ...
- PAT TOP 1001 Battle Over Cities - Hard Version (35)
问题描述: 1001 Battle Over Cities - Hard Version (35 分) It is vitally important to have all the cities c ...
- PAT TOP 1009. Triple Inversions (35)
问题描述: 1009. Triple Inversions (35) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Pen ...
最新文章
- Gartner:预计2018年人工智能行业总价值达1.2万亿美元
- 网络推广——移动端网络推广备受关注
- 基于docker部署的微服务架构(九): 分布式服务追踪 Spring Cloud Sleuth
- 如何将Pytorch生成的模型进行CPU部署
- 应用程序服务器迁移:从JBoss EE5到Wildfly EE7
- 【飞秋】Asp.net MVC2 model验证 看似美好,实则让人失望。
- 中国十大科技进展2项,世界十大科技进展6项生物相关;相比工程,生物与国际差距还是较大...
- php自动打印小票_服装店专用小票机自带进销存
- Google 要放弃 Android 了?
- 个人管理 - 我是这样偷着做架构的
- 小米6twrp最新第三方rec_小米5(gemini:双子座)刷机
- python整型变量化ascii_Python基础之基本数据类型
- 【android自定义控件】自定义Toast,AlterDialog,Notification 四
- cmos和ttl_TTL与CMOS详细介绍
- hermite插值matlab代码,hermite插值matlab
- linux ftp命令下载目录,linux命令行下的ftp 多文件下载和目录下载
- 宅男也可变形男-我是如何在11个月零27天减掉80斤的
- 计算机学院云毕业,“云端”相约,逐梦起航——计算机与设计学院举办2020届线上毕业典礼...
- 蒙提霍尔问题及其推广
- android 7.0 iso下载地址,iOS 7.0固件 全系列官方正式版下载地址