592 - Island of Logic

没解决这道题,会的帮一下忙吧

点击打开链接

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Island
{
    int speaker;
    int who;
    int what;
};
Island land[55];
char own[][25]= {" am divine."," am not divine.", " am evil."," am not evil.",
                 " am human."," am not human."," am lying."," am not lying.","t is day.","t is night."
                };
char other[][25]= {" is divine."," is not divine."," is evil."," is not evil.",
                   " is human."," is not human."," is lying."," is not lying."
                  };
char str[]= {'I','A','B','C','D','E'};
int n,flag,len,num[7][5];
Island creat(Island &p)
{
    char c,s[25];
    scanf("%c: ",&c);
    int j,m;
    for(j=1; j<6; j++)
        if(c==str[j])  break;
    num[j-1][0]=1;
    p.speaker=j;
    scanf("%c",&c);
    for(j=0; j<6; j++)
        if(c==str[j]) break;
    if(j>=1)
    {
        p.who=j;
        num[j-1][0]=1;
    }
    else p.who=p.speaker;
    memset(s,0,sizeof(s));
    gets(s);
    if(j==0)
    {
        for(m=0; m<10; m++)
            if(!strcmp(s,own[m])) break;
    }
    else for(m=0; m<8; m++)
            if(!strcmp(s,other[m])) break;
    if(m==8) p.who=6;
    else if(m==9) p.who=7;
    p.what=m+1;
    return p;
}
bool ly_or_not(int speaker,int what,int i)
{
    if(what==8)
    {
        if(speaker==1||(speaker==5&&!i)) return true;
        else return false;
    }
    if(what==7)
    {
        if(speaker==3||(speaker==6&&i)) return true;
        else return false;
    }
    if((speaker==(what+1)/2)==what%2) return true;
    return false;
}
bool check(int A,int B,int C,int D,int E,int i,int j)
{
    bool true_false;
    if(land[j].who>=6)
    {
        if((land[j].what-9==i)==land[j].what%2) true_false=true;
        else true_false=false;
    }
    else
    {
        if(land[j].who==1) true_false=ly_or_not(A,land[j].what,i);
        else if(land[j].who==2) true_false=ly_or_not(B,land[j].what,i);
        else if(land[j].who==3) true_false=ly_or_not(C,land[j].what,i);
        else if(land[j].who==4) true_false=ly_or_not(D,land[j].what,i);
        else true_false=ly_or_not(E,land[j].what,i);
    }
    if(true_false) switch(land[j].speaker)
        {
        case 1:
            if(A==1||(A==3&&!i)) return true;
            else return false;
        case 2:
            if(B==1||(B==3&&!i)) return true;
            else return false;
        case 3:
            if(C==1||(C==3&&!i)) return true;
            else return false;
        case 4:
            if(D==1||(D==3&&!i)) return true;
            else return false;
        case 5:
            if(E==1||(E==3&&!i)) return true;
            else return false;
        }
    else switch(land[j].speaker)
        {
        case 1:
            if(A==2||(A==3&&i)) return true;
            else return false;
        case 2:
            if(B==2||(B==3&&i)) return true;
            else return false;
        case 3:
            if(C==2||(C==3&&i)) return true;
            else return false;
        case 4:
            if(D==2||(D==3&&i)) return true;
            else return false;
        case 5:
            if(E==2||(E==3&&i)) return true;
            else return false;
        }
    return true;
}
void solve(Island *p)
{
    flag=0;
    for(int A=1; A<=3; A++)
        for(int B=1; B<=3; B++)
            for(int C=1; C<=3; C++)
                for(int D=1; D<=3; D++)
                    for(int E=1; E<=3; E++)
                        for(int i=0; i<2; i++)
                        {
                            int c=0;
                            for(int j=0; j<len; j++)
                                if(!check(A,B,C,D,E,i,j))
                                {
                                    c=1;
                                    break;
                                }
                            if(!c)
                            {
                                num[0][A]=1;
                                num[1][B]=1;
                                num[2][C]=1;
                                num[3][D]=1;
                                num[4][E]=1;
                                num[5][i]=1;
                            }
                        }
    if(!num[5][0]&&!num[5][1]) printf("This is impossible.\n");
    else
    {
        for(int i=0; i<5; i++)
            if(num[i][0])
            {
                int c;
                flag=0;
                for(int j=1; j<=3; j++)
                    if(num[i][j])
                    {
                        flag++;
                        c=j;
                    }
                if(flag>=2)
                {
                    printf("No facts are deducible.\n");
                    return;
                }
                else if(flag) num[i][0]=c;
                else
                {
                    printf("This is impossible.\n");
                    return;
                }
            }
        for(int i=0; i<5; i++)
            if(num[i][0])
            {
                printf("%c",'A'+i);
                if(num[i][0]==1) printf(" is divine.\n");
                else if(num[i][0]==2) printf(" is evil.\n");
                else printf(" is human.\n");
            }
        if(num[5][0]&&!num[5][1]) printf("It is day.\n");
        else if(!num[5][0]&&num[5][1]) printf("It is night.\n");
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.txt","r",stdin);
#endif
    int l=0;
    while(scanf("%d\n",&n)!=EOF)
    {
        if(!n) break;
        len=0;
        memset(num,0,sizeof(num));
        while(n--)
        {
            land[len]=creat(land[len]);
            len++;
        }
        printf("Conversation #%d\n",++l);
        solve(land);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值