Werewolf——困难版本

Werewolf(狼人杀) is a game in which theplayers are partitioned into two parties: the werewolves and the human beings.Suppose that in a game,

player #1 said: "Player #2 is awerewolf.";

player #2 said: "Player #3 is ahuman.";

player #3 said: "Player #4 is awerewolf.";

player #4 said: "Player #5 is ahuman."; and

player #5 said: "Player #4 is ahuman.".

Given that there were 2 werewolves amongthem, at least one but not all the werewolves were lying, and there wereexactly 2 liers. Can you point out the werewolves?

Now you are asked to solve a hardervertion of this problem: given that there were N players, with M werewolvesamong them, at least one but not all the werewolves were lying, and there wereexactly 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 statementof the i-th player (1 ≤ i ≤ N),which is represented by the index of the player with a positive sign for ahuman and a negative sign for a werewolf.

【Output Specification】

If a solution exists, print in a line indescending order the indices of the M werewolves. The numbers must be separatedby exactly one space with no extra spaces at the beginning or the end of theline. If there are more than one solution, you must output the largest solutionsequence -- 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. Incase there is no solution, simply print No Solution.

【Sample Input 1】

5 2 2

1 -2

2 +3

3 -4

4 +5

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

我的解法

#include<bits/stdc++.h>
using namespace std;
int record[100],iswolf[100];
int N,M,L,lier,lierwolf;
vector <int> wolf;

void DFS(int x)
{
    int i;
    
    if(wolf.size()==M)
    {
        lier=lierwolf=0;
        for(i=1;i<=N;i++)
        {
            if((record[i]>0&&iswolf[abs(record[i])]==1)||(record[i]<0&&iswolf[abs(record[i])]==0))
            {
                lier++;//the heart 
                if(iswolf[i]==1) lierwolf++;
            }
        }
        
        if(lier==L&&lierwolf>0&&lierwolf<M)
        {
            for(i=0;i<M;i++)
            {
                if(i==M-1)
                {
                    printf("%d",wolf[i]);
                }
                else
                {
                    printf("%d ",wolf[i]);
                }
            }
            exit(0);//哦哇利达 
        }
        return;//b t f
    }
    
    for(i=x;i>0;i--)
    {
        if(wolf.size()+i<M) break; 
        wolf.push_back(i);
        iswolf[i]=1;
        DFS(i-1);
        wolf.pop_back();
        iswolf[i]=0;
    }
}

int main()
{
    int i;
    
    scanf("%d %d %d",&N,&M,&L);
    //判断数据是否非法 
    if(N<5||N>100) 
    {
        return 0;
    }
    else if(M<2||M>N||L<2||L>N) 
    {
        return 0;
    }
    //初始化 
    for(i=1;i<=N;i++)
    {
        record[i]=0;
    }
    //主程序 
    for(i=1;i<=N;i++) 
    {
        scanf("%d", &record[i]);
    }
    DFS(N);
    printf("No solutiong");
}

思路DFS剪枝

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值