PAT 1148 Werewolf - Simple Version(20 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CillyB/article/details/82557821

题意:有n个人,其中有两个狼人,其他都是好人,现在每个人都会说某人是好人还是坏人。其中有两个人会说谎,一个狼人一个好人。现在让你找出哪俩人是狼人,若不存在则输出No Solution。(n <= 100)

样例:

5

-2

+3

-4

+5

+4

输出:

1 4

思路:因为n很小,所以两个for暴力枚举说谎的人,这样假定哪两个说谎之后,就可以去验证下,是否是两个狼人,并且一个狼人说谎一个好人说谎。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e3+5;
int a[maxn];
int book[maxn];

int main(void)
{
    int n;
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        bool ok = 0;
        int ans1 = 0, ans2 = 0;
        for(int i = 1; i <= n && !ok; i++)
            for(int j = i+1; j <= n && !ok; j++)
            {
                memset(book, 0, sizeof(book));
                book[i] = book[j] = 1;
                int lieA = 0, lieB = 0;
                for(int k = 1; k <= n; k++)
                {
                    if(!book[k])    //ren
                    {
                        if(a[k] < 0 && book[-a[k]] == 0) lieA++;
                        if(a[k] > 0 && book[a[k]] == 1) lieA++;
                    }
                    else
                    {
                        if(a[k] < 0 && book[-a[k]] == 0) lieB++;
                        if(a[k] > 0 && book[a[k]] == 1) lieB++;
                    }
                }
                if(lieA == 1 && lieB == 1)
                {
                    ok = 1;
                    ans1 = i, ans2 = j;
                }
            }
        if(ok) cout << ans1 << ' ' << ans2 << endl;
        else puts("No Solution");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CillyB/article/details/82557821