PTA Werewolf

PTA Werewolf

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.”;
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 vertion of this problem: given that there were N players, with M werewolves among them, at least one but not all the werewolves were lying, and there were exactly 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 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 descending order the indices of the M 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 largest solution sequence – 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. In case there is no solution, simply print No Solution.

Sample Input 1:

5 2 2
-2
+3
-4
+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

思路:

根据题目我们不难发现,最后的输出结果是狼的标号。所以我们以找到狼作为我们的根本目标。
程序大致按照以下的思路进行:

  1. 产生n玩家,m匹狼的组合数。
  2. 验证每种情况是否符合说谎人数。一旦超过人数就立即检查下一组。
  3. 验证是否只有1m-1匹狼说谎。
  4. 输出最终解答。

代码实现:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

void backtrack(int n, int start, int m, vector<int>& path);

vector<vector<int>> res; // to store the combine


vector<vector<int>> combine(int n, int m) {
    
    
    vector<int> path;
    backtrack(n, n, m, path);
    return res;
} // compute the combine

void backtrack(int n, int start, int m, vector<int>& path) {
    
    
    if (path.size() == m) {
    
    
        res.push_back(path);
        return;
    }
    for (int i = start; i >= 1; --i) {
    
    
        path.push_back(i);
        backtrack(n, i-1, m, path);
        path.pop_back();
    }
}

int main() {
    
    
    int n, m, l; // n for the total number of players, m for wolves, and l for liars
    int m_count, l_count; // temporary to count wolves and liars
    cin >> n >> m >> l;
    m_count = m;
    l_count = l;

    vector<int> ass(n+1);
    vector<vector<int>> result;
    vector<int> pre(n+1, 1);

    int t;
    for (int i = 0; i < n; i++) {
    
     // get players's assertions
        cin >> t;
        ass[i+1] = t;
    }

    combine(n, m);

    int count = 0;
    for (auto one: res) {
    
    
        for (auto i: one) {
    
    
            pre[i] = -1;
        }
        for (int i = 1; i <= n; i++) {
    
    
            if (ass[i] * pre[abs(ass[i])] < 0) {
    
    
                if (pre[i] == -1)
                    m_count--;
                l_count--;
            }

        }
        pre.assign(n + 1, 1);
        if ((l_count != 0) || (m_count == 0) || (m_count == m)) {
    
    
            l_count = l;
            m_count = m;
            count ++;
            continue;
        } else {
    
    
            result.push_back(one);
            l_count = l;
            m_count = m;
        }

    }

    if (count != res.size()) {
    
    
        int s = result[0].size();
        sort(result[0].rbegin(), result[0].rend());
        for (int i = 0; i < s - 1; ++i) {
    
    
            cout << result[0][i] << ' ';
        }
        cout << result[0][s - 1];
    }
    else {
    
    
        cout << "No Solution";
    }
}

猜你喜欢

转载自blog.csdn.net/LordTech/article/details/105232414
PTA