問題描述:
1022. Werewolf (35) 時間限制 100 ms
記憶體限制 65536 kB
代碼長度限制 8000 B
判題程式 Standard 作者 CHEN, Yue
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代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
#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 |