#1148. Werewolf - Simple Version【枚举 + 模拟】

原题链接

Problem Description:

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.”;
  • payer #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 version of this problem: given that there were N N N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 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 a positive integer N N N ( 5 ≤ N ≤ 100 5\leq N\leq 100 5N100). Then N N N lines follow and the i-th line gives the statement of the i-th player ( 1 ≤ i ≤ N 1\leq i\leq N 1iN), 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 ] A=a[1],\ldots,a[M] A=a[1],,a[M] and B = b [ 1 ] , … , b [ M ] B=b[1],\ldots,b[M] B=b[1],,b[M], if there exists 0 ≤ k < M 0\leq k<M 0k<M such that a [ i ] = b [ i ] a[i]=b[i] a[i]=b[i] ( i ≤ k i\leq k ik) and a [ k + 1 ] < b [ k + 1 ] a[k+1]<b[k+1] a[k+1]<b[k+1], then A A A is said to be smaller than B B 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

Problem Analysis:

PAT 中题面涉及到推理的大多都是枚举题,不需要思考过于复杂,将答案推理出来,可以直接进行枚举。

由于两人有且只有两个,我们可以设置两重循环枚举所有的狼人的可能情况,然后根据题目所给条件进行判断即可。具体细节见代码。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 110;

int n;
int q[N];

inline int judge(int k, int i, int j)  // 说谎返回1,真话返回0
{
    
    
    int t = q[k];
    if (t > 0)
    {
    
    
        if (t == i || t == j) return 1;
        return 0;
    }
    
    t = -t;
    if (t == i || t == j) return 0;
    return 1;
}

int main()
{
    
    
    cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> q[i];

    for (int i = 1; i <= n; i ++ )  // 枚举两个狼人是谁(i, j),两重循环
        for (int j = i + 1; j <= n; j ++ )
        {
    
    
            int s = judge(i, i, j) + judge(j, i, j);  // 当前两只狼是 i, j
            if (s != 1) continue;  // 有且只有一只狼说谎不成立
            
            s = 0;  // 枚举所有人
            for (int k = 1; k <= n; k ++ )
                s += judge(k, i, j);
            
            if (s != 2) continue;  // 只有两人说谎的条件不成立
            
            cout << i << ' ' << j << endl;
            return 0;
        }
    
    puts("No Solution");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121188709