PAT甲级1148 Werewolf - Simple Version (20分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input 1:

5
-2
+3
-4
+5
+4

Sample Output 1:

1 4

Sample Input 2:

6
+6
+3
+1
-5
-2
+4

Sample Output 2(the solution is not unique):

1 5

Sample Input 3:

5
-2
-3
-4
-5
-1

Sample Output 3:

No Solution

二、解题思路

狼人杀,比较新颖的题目,抽象出来其实并不难,我们可以建立一个二重循环,每一次假设两个人是狼人,然后整体遍历一次,看是不是有两个人说谎,而且其中一个是狼人,如果满足这个条件,直接输出,然后return,否则继续循环即可。详情见代码注释。

三、AC代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 101;
int words[maxn], identity[maxn];    //存放每个人说的,存放每个人的身份
int main()
{
    
    
    int N, liar, lie_w;
    scanf("%d", &N);
    for(int i=1; i<=N; i++)
    {
    
    
        scanf("%d", &words[i]); //每个人说的
    }
    fill(identity, identity+N+1, 1);    //将每个人都初始化为好人
    for(int i=1; i<=N; i++)
    {
    
    
        for(int j=i+1; j<=N; j++)
        {
    
    
            liar = 0;
            lie_w = 0;
            identity[i] = identity[j] = -1; //假设遍历到的i j两个是狼人
            for(int k=1; k<=N; k++)
            {
    
    
                if(words[k] * identity[abs(words[k])] < 0)  //有人在说谎
                {
    
    
                    if(k == i || k== j) lie_w++;    //说谎的人是狼人
                    else    liar++; //说谎的正常人
                }
            }
            if(liar == 1 && lie_w == 1) //有两个人说谎,且一个人是狼人
            {
    
    
                printf("%d %d\n", i, j);    //输出
                return 0;
            }
            identity[i] = identity[j] = 1;
        }
    }
    printf("No Solution\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/109092287