zoukankan      html  css  js  c++  java
  • 1148 Werewolf

    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). Then N lines follow and the i-th line gives the statement of the i-th player (1), 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 [ and [, if there exists 0 such that [ (i≤k) and [, 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

    题意:

      有N个人,这里面有两只是狼人,一只狼人说真话,另一只狼人说假话,另外还有一个人类说假话,判断那两个人是狼人。

    思路:

      看大佬写的吧~

    Code:

    #include<iostream>
    #include<vector>
    #include<cmath>
    #include<set>
    #include<algorithm>
    
    using namespace std;
    
    bool cmp(pair<int, int> a, pair<int, int> b) {
        if (a.first == b.first) {
            return a.second < b.second;
        } else {
            return a.first < b.first;
        }
    }
    
    int main() {
        int n, t;
        cin >> n;
    
        set<int> human;
        set<int> werewolf;
        vector<int> said(n+1, 0);
        vector<pair<int, int> > twoWereWolf;
    
        for (int i = 1; i <= n; ++i) {
            cin >> t;
            said[i] = t;
        }
    
        bool wrong = false;
        int werewolf1, werewolf2;
        bool foundWerewolf1 = false;
        bool foundWerewolf2 = false;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (j == i) continue;
                werewolf1 = -1;
                werewolf2 = -1;
                wrong = false;
                foundWerewolf1 = false;
                foundWerewolf2 = false;
                human.clear();
                werewolf.clear();
                for (int k = 1; k <= n; ++k) {
                    if (k != i && k != j) {
                        if (said[k] < 0)
                            werewolf.insert(abs(said[k]));
                        else 
                            human.insert(said[k]);
                    }
                }
                for (int h : human) {
                    if (h == i) {
                        werewolf1 = j;
                        foundWerewolf1 = true;
                    }
                    if (h == j) {
                        werewolf1 = i;
                        foundWerewolf1 = true;
                    }
                }
                for (int w : werewolf) {
                    if (w != werewolf1) {
                        werewolf2 = w;
                        foundWerewolf2 = true;
                    }
                }
                if (foundWerewolf1 && foundWerewolf2) {
                    if (human.find(foundWerewolf1) == human.end() && human.find(foundWerewolf2) == human.end())
                        if (werewolf.find(werewolf1) != werewolf.end() || werewolf.find(werewolf2) != werewolf.end())
                            twoWereWolf.push_back({werewolf1, werewolf2});
                }
            }
        }
    
        sort(twoWereWolf.begin(), twoWereWolf.end(), cmp);
    
        if (twoWereWolf.size() > 0) {
            cout << twoWereWolf[0].first << " " << twoWereWolf[0].second << endl;
        } else {
            cout << "No Solution" << endl;
        }
    
        return 0;
    }

    记得上上次考试的时候我没有做出来这道题,今天又做了一下,只通过了两组样例。


    大佬的代码:https://www.liuchuo.net/archives/6494

    #include <iostream>
    #include <vector>
    #include <cmath>
    using namespace std;
    int main() {
        int n;
        cin >> n;
        vector<int> v(n+1);
        for (int i = 1; i <= n; i++) cin >> v[i];
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                vector<int> lie, a(n + 1, 1);
                a[i] = a[j] = -1;
                for (int k = 1; k <= n; k++)
                    if (v[k] * a[abs(v[k])] < 0) lie.push_back(k);
                if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) {
                    cout << i << " " << j;
                    return 0;
                }
            }
        }
        cout << "No Solution";
        return 0;
    }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    如何发现需求
    测试linux和window下 jdk最大能使用多大内存
    java获取汉字的拼音 简单版
    oracle一条sql执行导入sql文件
    oracle使用闪回功能恢复删除的表数据
    linux环境变量配置
    有两张表;使用SQL查询,查询所有的客户订单日期最新的前五条订单记录。 糖不苦
    jQuery作业 点击出弹框 糖不苦
    #{}和${}的区别是什么? 糖不苦
    在html页面中如何使用jQuery? 糖不苦
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12682210.html
Copyright © 2011-2022 走看看