【PAT甲级】1148 Werewolf - Simple Version (20 分)(枚举,思维)

版权声明:本人大三在读,有错误烦请指正,共同进步- ( ゜- ゜)つロ 乾杯~点赞请按右上角,转载请标明出处: https://blog.csdn.net/hzyhfxt/article/details/87974887

学习博客链接 

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 version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 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 a positive integer N (5≤N≤100). 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 ascending order the indices of the two 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 smallest solution sequence — that is, for two sequences A=a[1],…,a[M] and B=b[1],…,b[M], if there exists 0≤k[k+1]<b[k+1], then A is said to be smaller than B. In case there is no solution, simply print No Solution.

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

【分析】

题意是各有一个狼人和一个好人说谎,

两个循环枚举两个狼人的位置,

然后假设情况一:狼人说谎,(说谎情况一、陈述a[k]是狼人但不满足a[k]位置是i 或者j,二、陈述a[k]是好人但是a[k]是狼人,满足==i 或者==j),则说谎狼人数量+1

情况二:好人说谎,(满足条件和情况一相同)则说谎好人数量+1

判断满足这两种情况各自的数量,都各自为1时找到答案,此时枚举的ij就是狼人的位置

#include <cstdio>
using namespace std;
int a[105];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++)
        scanf("%d",&a[i]);
   
    for(int i = 1;i <= n;i++)
    {
        
        for(int j = i+1;j <= n;j++)
        {
            int lie_wolf = 0,lie_man = 0;
            for(int k = 1; k  <= n;k++)
            {
                if(k == i || k == j)//假设 i和j是狼人,狼人说谎
                {
                    if(a[k] < 0 && -a[k] != i && -a[k] != j )//陈述中说a[k]是狼人,则a[k]必须是好人
                        lie_wolf++;
                    if(a[k] > 0 && (a[k] == i || a[k] == j))//陈述中a[k]是好人,则a[k]必须是狼人i或狼人j
                        lie_wolf++;
                }
                else//好人撒谎
                {
                    if(a[k] < 0 && -a[k] != i && -a[k] != j )//陈述中说a[k]是狼人,则a[k]必须是好人
                        lie_man++;
                    if(a[k] > 0 && (a[k] == i || a[k] == j))//陈述中a[k]是好人,则a[k]必须是狼人i或狼人j
                        lie_man++;
                }
            }
            if(lie_man == 1 && lie_wolf == 1)
            {
                printf("%d %d\n",i,j);
                return 0;
            }
        }
        
    }
   printf("No Solution\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/87974887