题目描述
以下文字摘自《灵机一动·好玩的数学》:“狼人杀”游戏分为狼人、好人两大阵营。在一局“狼人杀”游戏中,1号玩家说:“2号是狼人”,2号玩家说:“3号是好人”,3号玩家说:“4号是狼人”,4号玩家说:“5号是好人”,5号玩家说:“4号是好人”。已知这5名玩家中有2人扮演狼人角色,有2人说的不是实话,有狼人撒谎但并不是所有狼人都在撒谎。扮演狼人角色的是哪两号玩家?
本题是这个问题的升级版:已知 N 名玩家中有 M 人扮演狼人角色,有 L 人说的不是实话,有狼人撒谎但并不是所有狼人都在撒谎。要求你找出扮演狼人角色的是哪几号玩家?
输入格式:
输入在第一行中给出三个正整数 N、M、L,其中 5 ≤ N ≤ 100,2 ≤ M,L < N。随后 N 行,第 i 行给出第 i 号玩家说的话(1 ≤ i ≤ N),即一个玩家编号,用正号表示好人,负号表示狼人。
输出格式:
如果有解,在一行中按递减顺序输出 M 个狼人的编号,其间以空格分隔,行首尾不得有多余空格。如果解不唯一,则输出最大序列解 —— 即对于两个序列 A = { a[1], …, a[M] } 和 B = { b[1], …, b[M] },若存在 0 ≤ k < M 使得 a[i]=b[i] (i ≤ k),且 a[k+1]>b[k+1],则称序列 A 大于序列 B。若无解则输出 No Solution。
输入样例 1:
5 2 2
-2
+3
-4
+5
+4
输出样例 1:
4 1
输入样例 2:
6 2 3
-2
+3
-4
+5
+4
-3
输出样例 2(解不唯一):
6 4
输入样例 3:
6 2 5
-2
+3
-4
+5
+4
+6
输出样例 3:
No Solution
注意点:狼人中必须存在说谎的
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct person{
int c;
int other;
};
person p[101];//认为
int cself[101]={0};//真实身份,-1狼,1人
int say[101]={0};//1假,-1真
int N,M,L;
bool cmp(vector<int>v1,vector<int>v2){
for(int i=0;i<v1.size();i++){
if(v1[i]!=v2[i])return v1[i]>v2[i];
}
return v1[0]>v2[0];
}
bool judge(int i,int x){
if(x==1){//真
if(cself[p[i].other]==0||(p[i].c==cself[p[i].other]))return true;
else if(p[i].c!=cself[p[i].other]==1)return false;
}else{//假
if(cself[p[i].other]==0||(p[i].c!=cself[p[i].other]))return true;
else if(p[i].c==cself[p[i].other])return false;
}
}
int summ=0;
vector<int>v[1000];
bool wolf(int i,int m,int l){
if(m<0||l<0||i<l)return false;
if(i==0&&m==0&&l==0){
int flag=0;
for(int j=N;j>=1;j--){
if(cself[j]==-1){
v[summ].push_back(j);
if(say[j]==-1)flag=1;
}
}
if(flag==1)summ++;
else{
v[summ].clear();
}
return true;
}
if(i==0&&l==0&&m>0){
int sum=0;
for(int j=1;j<=N;j++){
if(cself[j]==0)sum++;
}
if(sum>=m){
sum=0;
int flag=0;
for(int j=N;j>=1;j--){
if(cself[j]==-1){
v[summ].push_back(j);
if(say[j]==-1)flag=1;
}else if(cself[j]==0&&sum<m){
v[summ].push_back(j);
if(cself[j]==0){
sum++;
if(say[j]==-1)flag=1;
}
}
}
if(flag==1)summ++;
else v[summ].clear();
return true;
}else return false;
}
if(i==0)return false;
if(judge(i,1)){
say[i]=1;
if(cself[p[i].other]==0&&p[i].c<0&&m>0){
cself[p[i].other]=-1;
wolf(i-1,m-1,l);
cself[p[i].other]=0;
}else if(p[i].c>0&&cself[p[i].other]==0){
cself[p[i].other]=1;
wolf(i-1,m,l);
cself[p[i].other]=0;
}else if(cself[p[i].other]!=0){
wolf(i-1,m,l);
}
}
if(judge(i,-1)&&l>0){
say[i]=-1;
if(cself[p[i].other]==0&&p[i].c<0){
cself[p[i].other]=1;
wolf(i-1,m,l-1);
cself[p[i].other]=0;
}else if(p[i].c>0&&cself[p[i].other]==0&&m>0){
cself[p[i].other]=-1;
wolf(i-1,m-1,l-1);
cself[p[i].other]=0;
}else if(cself[p[i].other]!=0){
wolf(i-1,m,l-1);
}
}
}
int main(){
cin>>N>>M>>L;
for(int i=1;i<=N;i++){
char c;
cin>>c>>p[i].other;
if(c=='-')p[i].c=-1;
else p[i].c=1;
}
wolf(N,M,L);
if(summ>0){
sort(v,v+summ,cmp);
for(int i=0;i<v[0].size();i++){
if(i!=0)cout<<" ";
cout<<v[0][i];
}
}else{
cout<<"No Solution";
}
}