PAT甲級1148 Werewolf - Simple Version (20分) 和乙級狼人殺-簡單版(20 分) 關鍵是思考切入的角度

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章