PAT TOP 1022. Werewolf (35)

Source: Internet
Author: User
Tags time limit

Problem Description:

1022. Werewolf ( time limit)
Memory Limit 65536 KB
Code length limit 8000 B
Program Standard author CHEN, Yue

Werewolf (Werewolf Kill) is a game in which the players was partitioned into the parties:the werewolves and the human beings. Suppose. 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 werewolves?

Now is asked to solve a harder vertion of this problem:given the 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 is supposed to point out the the werewolves.

Input Specification:

Each input file contains the one test case. For each case, the first line gives three positive integer N (5 <= N <=), 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 was 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 is separated by exactly one space with no extra spaces at the beginning or the end of the line. If there is more than one solution, you must output the largest solution sequence--that's, for-double 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 was said to being 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 (the solution is not unique):
6 4
Sample Input 3:
6 2 5
-2
+3
-4
+5
+4 +6
Sample Output 3:
No Solution

This problem is also a violent dfs+ pruning algorithm can be solved, just to determine the conditions a little complex ...

Four variables were maintained during DFS search: (1) The remaining number of people who did not decide to lie, N, (2) The number of people who had been convicted of lying, LN, (3) The number of werewolves who had been judged to be werewolf, (4) The number of un-identified persons; plus some basic pruning conditions, you can AC ...

AC Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5
 9 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 12 0 121 122 123 124 126 127 (129) 131 133 134 135 136 137 138 139 141 142 143 144 145 
#include <bits/stdc++.h> using namespace std;
Vector<int> V,vr,vl,vi;
int n,m,k; void Dfs (int n,int ln,int wn,int un) {if (ln>k| | wn>m| |
	WN+UN&LT;M) return;
	else if (N&LT;K-LN) return;
			else if (n<1) {if (ln==k&&wn<=m&&wn+un>=m) {int first=0;
			BOOL Flag=false;
			int fl=0;
			for (int i=v.size () -1;i>0&&!flag;--i) {if (vl[i]==-1&&vi[i]==-1) flag=true; 
					} if (M-WN) for (int i=v.size () -1;i>0&&!flag;--i) {if (vl[i]==-1&&vi[i]==0) {fl=i;
					Vi[i]=-1;
					wn++;
					un--;
				Flag=true;
				}} if (flag) {vector<int> VRR;					
				for (int i=v.size () -1;i>0&&vrr.size () <m;--i) {if (vi[i]==-1) vrr.emplace_back (i);
				} for (int i=v.size () -1;i>0&&vrr.size () <m;--i) {if (vi[i]==0) vrr.emplace_back (i);
				} sort (Vrr.begin (), Vrr.end (),greater<int> ());
				if (Vr.empty ()) VR=VRR;else for (int i=0;i<vrr.size (); ++i) {if (Vrr[i]>vr[i]) {VR=VRR;
					Break
		}}} vi[fl]=0;
		}} else {int vln=vl[n];
		int Vn=abs (v[n]);

		int VIVN=VI[VN];
				if (v[n]>0) {if (vi[vn]<0) {vl[n]=-1;
			DFS (N-1,ln+1,wn,un);
				} else if (vi[vn]>0) {vl[n]=1;
			DFS (N-1,ln,wn,un);
				} else {vl[n]=1;
				Vi[vn]=1;
				DFS (N-1,LN,WN,UN-1);
				Vl[n]=-1;
				Vi[vn]=-1; DFS (N-1,LN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.