7-5 Werewolf (PAT ADSAA)

该代码实现了一个狼人杀逻辑解谜的程序,基于回溯算法找出在给定条件下狼人(werewolves)的身份。程序接收玩家声明,处理含有一定数量的狼人、人类以及说谎者的场景,并输出可能的狼人序列。当没有解决方案时,输出NoSolution。
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

int N, M, L;
char c;
std::vector<std::pair<int, int>> claim;
std::vector<int> flag;
std::vector<int> curr;
std::vector<std::vector<int>> 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<int> &a, const std::vector<int> &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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值