1148 Werewolf - Simple Version (20分)
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 liars. Can you point out the werewolves?
Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. You are supposed to point out the werewolves.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (5≤N≤100). 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 ascending order the indices of the two 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 smallest 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 smaller than B. In case there is no solution, simply print No Solution.
Sample Input 1:
5
-2
+3
-4
+5
+4
Sample Output 1:
1 4
Sample Input 2:
6
+6
+3
+1
-5
-2
+4
Sample Output 2 (the solution is not unique):
1 5
Sample Input 3:
5
-2
-3
-4
-5
-1
Sample Output 3:
No Solution
这个题主要是思考的角度,我最初是考虑把两个说谎的人先固定下来,然后根据其他说实话人的情况判断所有编号是狼是人,但是可能会有的编号并没有涉及,没人说他们是狼,也没人说他们是人,太麻烦了。
好的办法是确定两个狼是谁,这样我们其实立马就知道了所有编号是狼是人的情况了,这样的话就很顺利成章的判断谁说谎了,然后对说谎人进行判断(需要一个狼一个人就好)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <cmath>
using namespace std;
struct node
{
int a;//小的狼人编号
int b;//大的狼人编号
};
int sort2(node a,node b){
if(a.a!=b.a)//先判断小的编号是否一样,不一样那么顺序就是小编号的一组靠前
return a.a<b.a;
else
return a.b<b.b;
}
int main()
{
int N;
scanf("%d",&N);
vector<node> lastOut;
int sn[N+1];
for(int i=1; i<=N; i++)
{
scanf("%d",&sn[i]);
}
for(int m=1; m<=N; m++)
{
for(int n=1; n<=N; n++)
{
// m n的index是 两个狼的标号
int out =1;
int sign[N+1];//标志位1 是狼 2是人
fill(sign,sign+N+1,2);
sign[m]=1;
sign[n]=1;
//所有狼人情况保存在了sign数组之中了,1是狼 2是人
//下面就就是判断那个人说谎了
unordered_set<int> sett;//记录那个index说谎了
for(int i=1; i<=N; i++)
{
int get=sn[i];
if(get>0) //认为get这个编号是人
{
if(sign[get]!=2)
sett.insert(i);
}
else //认为get这个编号是狼
{
get=get*-1;
if(sign[get]!=1)
sett.insert(i);
}
}
if(sett.size()!=2)
{
out=0;
}
else
{
//sett.find(m)!=sett.end() ture的话,就是m说谎了,m在说谎名单里
bool signM=(sett.find(m)!=sett.end());
bool signN=(sett.find(n)!=sett.end());
if(signM&&signN)//两个狼人都说谎了
{
out=0;
}
else if(!(signM||signN))//两个狼人都没说谎
{
out=0;
}
}
if(out==1)
{
node a;
if(m<n)
{
a.a=m;
a.b=n;
}
else
{
a.a=n;
a.b=m;
}
lastOut.push_back(a);
}
}
}
if(lastOut.size()>0){
sort(lastOut.begin(),lastOut.end(),sort2);
cout<<lastOut[0].a<<' '<<lastOut[0].b;
}
else
cout<<"No Solution";
return 0;
}