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
在甲级题目本身上又升级了,werewolf 和 liers 的数目都不确定 ,不过也是DP题目,只不过以前可以两个for循环搞定,现在得正儿八经写个递归出来
思路是遍历整个解空间,对于这道题而言就是狼人的编号,因为要求多个结果输出较大的编号,我们从编号M-1开始,用递归遍历(解空间不太好理解的话可以画出树帮助理解),当找到M个狼人是,进入Judge函数
判断的思路是,根据狼人的编号,可以得出所有人的身份(狼1人-1,数组下标表示编号),遍历下标,根据读入的statement判断,谁在说谎。得到lier数组后,判断是否符合题意。
如果对BackTracking回溯不太理解的话,可以画出解空间的树形图,自己在把search 和 back的路线走一次就懂了
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int a