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


被折叠的 条评论
为什么被折叠?



