7-5 Werewolf (PAT ADSAA)

#include 
#include 
#include 
#include 

int N, M, L;
char c;
std::vector> claim;
std::vector flag;
std::vector curr;
std::vector> res;

bool judge() {
  int lieWolf = 0;
  int liers = 0;
  for (int i = 1; i <= N; ++i) {
    if (claim[i].second != flag[claim[i].first]) {
      if (flag[i] == -1) {
        ++lieWolf;
      }
      ++liers;
    }
  }
  if (lieWolf == 0 || lieWolf == M || liers != L) {
    return false;
  }
  return true;
}

void backtracking(int k) {
  if (curr.size() + k - 1 < M) {
    return;
  }
  if (k == 1 && curr.size() != M) {
    return;
  }
  if (curr.size() == M) {
    if (judge()) {
      res.push_back(curr);
    }
    return;
  }
  flag[k - 1] = -1;
  curr.push_back(k - 1);
  backtracking(k - 1);
  flag[k - 1] = 1;
  curr.pop_back();
  backtracking(k - 1);
}

bool cmp(const std::vector &a, const std::vector &b) {
  int sz = a.size() > b.size() ? b.size() : a.size();
  for (int i = 0; i < sz; ++i) {
    if (a[i] > b[i]) {
      return true;
    }
    if (a[i] < b[i]) {
      return false;
    }
  }
  return false;
}

int main() {
  std::cin >> N >> M >> L;
  claim.resize(N + 1);
  for (int i = 1; i <= N; ++i) {
    std::cin >> c >> claim[i].first;
    if (c == '+') {
      claim[i].second = 1;
    } else {
      claim[i].second = -1;
    }
  }
  flag.resize(N + 1, 1);
  flag[N] = -1;
  curr.push_back(N);
  backtracking(N);
  flag[N] = 1;
  curr.pop_back();
  backtracking(N);
  if (res.size() == 0) {
    std::cout << "No Solution";
  } else {
    sort(res.begin(), res.end(), cmp);
    for (int i = 0; i < res[0].size(); ++i) {
      std::cout << res[0][i];
      if (i != res[0].size() - 1) {
        std::cout << " ";
      }
    }
  }
  return 0;
}

题目如下:

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 < N, 1 ≤ 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

你可能感兴趣的:(PAT,ADSAA,c++,算法,pat考试)