zoukankan      html  css  js  c++  java
  • PAT甲级——A1148 WerewolfSimpleVersion【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). Then Nlines 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 [ (ik) 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

    Solution:

      mmp,看了半天题目,以为要用一个复杂算法来推导,半天想不出来,后来才发现,20分的题,你千万不要看高了他,该暴力就暴力,该遍历就遍历,不要想用什么高大上的算法!!!

      这道题就是使用暴力遍历,从1开始,每两个个人假设为狼人,然后去验证几个人说谎了,并且是不是一个好人和一个狼人说谎了,对,就这么简单,就他妈的用遍历!!!

      

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int n;
     8     cin >> n;
     9     vector<int>speak(n + 1), name(n + 1, 0);
    10     for (int i = 1; i <= n; ++i)cin >> speak[i];
    11     for (int i = 1; i <= n; ++i)//假设i,j是狼人
    12     {
    13         for (int j = i + 1; j <= n; ++j)
    14         {
    15             vector<int>lie, v(n + 1, 1);//都是好人
    16             v[i] = v[j] = -1;//标记狼人
    17             for (int t = 1; t <= n; ++t)
    18                 if (v[abs(speak[t])] * speak[t] < 0)//说是好人,但实际是狼人,他说谎了
    19                     lie.push_back(t);
    20             if (lie.size() == 2 && v[lie[0]] + v[lie[1]] == 0)//一个好人撒谎,一个坏人撒谎
    21             {
    22                 cout << i << " " << j << endl;
    23                 return 0;
    24             }
    25         }
    26     }
    27     cout << "No Solution" << endl;
    28     return 0;
    29 }
  • 相关阅读:
    slf4j+log4j的使用
    <context:component-scan>详解
    Spring装配Bean---使用xml配置
    Spring应用上下文中Bean的生命周期
    bootstrap table 复选框选中后,翻页之后保留先前选中数据
    前后端分离的时代,如何解决前后端接口联调问题?
    利用vue-cli搭建vue项目
    vue之注册自定义的全局js函数
    小程序之图片上传
    微信小程序-蓝牙连接
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11920466.html
Copyright © 2011-2022 走看看