Werewolf

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
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 vector<int> v,vr,vl,vi;
  4 int n,m,k;
  5 void dfs(int n,int ln,int wn,int un)
  6 {
  7     if(ln>k||wn>m||wn+un<m)
  8     return;
  9     else if(n<k-ln)
 10     return;
 11     else if(n<1)
 12     {
 13         if(ln==k&&wn<=m&&wn+un>=m)
 14         {
 15             int first=0;
 16             bool flag=false;
 17             int fl=0;
 18             for(int i=v.size()-1;i>0&&!flag;--i)
 19             {
 20                 if(vl[i]==-1&&vi[i]==-1)
 21                 flag=true;
 22             }
 23             if(m-wn)
 24             for(int i=v.size()-1;i>0&&!flag;--i)
 25             {
 26                 if(vl[i]==-1&&vi[i]==0)
 27                 {
 28                     fl=i;
 29                     vi[i]=-1;
 30                     wn++;
 31                     un--;
 32                     flag=true;
 33                 }
 34             }
 35             if(flag)
 36             {
 37                 vector<int> vrr;
 38                 for(int i=v.size()-1;i>0&&vrr.size()<m;--i)
 39                 {
 40                     if(vi[i]==-1)
 41                     vrr.emplace_back(i);                    
 42                 }
 43                 for(int i=v.size()-1;i>0&&vrr.size()<m;--i)
 44                 {
 45                     if(vi[i]==0)
 46                     vrr.emplace_back(i);
 47                 }
 48                 sort(vrr.begin(),vrr.end(),greater<int>());
 49                 if(vr.empty())
 50                 vr=vrr;
 51                 else
 52                 for(int i=0;i<vrr.size();++i)
 53                 {
 54                     if(vrr[i]>vr[i])
 55                     {
 56                         vr=vrr;
 57                         break;
 58                     }
 59                 }
 60             }
 61             vi[fl]=0;
 62         }
 63     }
 64     else
 65     {
 66         
 67         int vln=vl[n];
 68         int vn=abs(v[n]);
 69         int vivn=vi[vn];
 70 
 71         if(v[n]>0)
 72         {
 73             if(vi[vn]<0)
 74             {
 75                 vl[n]=-1;
 76                 dfs(n-1,ln+1,wn,un);
 77             }
 78             else if(vi[vn]>0)
 79             {
 80                 vl[n]=1;
 81                 dfs(n-1,ln,wn,un);
 82             }
 83             else
 84             {
 85                 vl[n]=1;
 86                 vi[vn]=1;
 87                 dfs(n-1,ln,wn,un-1);
 88                 vl[n]=-1;
 89                 vi[vn]=-1;
 90                 dfs(n-1,ln+1,wn+1,un-1);
 91             }
 92         }
 93         else
 94         {
 95             if(vi[vn]<0)
 96             {
 97                 vl[n]=1;
 98                 dfs(n-1,ln,wn,un);
 99             }
100             else if(vi[vn]>0)
101             {
102                 vl[n]=-1;
103                 dfs(n-1,ln+1,wn,un);
104             }
105             else
106             {
107                 vl[n]=1;
108                 vi[vn]=-1;
109                 dfs(n-1,ln,wn+1,un-1);
110                 vl[n]=-1;
111                 vi[vn]=1;
112                 dfs(n-1,ln+1,wn,un-1);
113             }    
114         }
115         vi[vn]=vivn;
116         vl[n]=vln;
117         return;
118     }
119 }
120 int main()
121 {
122 //    freopen("data.txt","r",stdin);
123     ios::sync_with_stdio(false);
124     int x;
125     cin>>n>>m>>k;
126     vl.resize(n+1,0);
127     vi.resize(n+1,0);
128     v.push_back(0);
129     for(;n--;)
130     {
131         cin>>x;
132         v.push_back(x);
133     }
134     dfs(v.size()-1,0,0,v.size()-1);
135     
136     if(vr.empty())
137     cout<<"No Solution";
138     else
139     {
140         cout<<vr[0];
141         for(int i=1;i<vr.size();++i)
142         cout<<" "<<vr[i];
143     }
144     return 0;
145 }

 

posted @ 2020-02-09 00:31  一斜星辰酱  阅读(425)  评论(0编辑  收藏  举报