UVA592字符串推断题加暴力假设搜索

63 篇文章 0 订阅
52 篇文章 0 订阅

这题稍微有些复杂,当时想了很久没敢下手敲,主要还是代码能力不够,有了基本的思想不敢敲,

而这道题实际上就是暴力搜索,因为只有五个人加上天气一共六个,直接假设就行了。

这里用一个birth数组存储对话过程中曾经出现的人物。然后结果肯定是通过出现过的人物然后假设就行了。

同时注意如果搜索的所有情况均不可行,说明对话有错误,即是第一种情况。此外有一种情况成立并不能说明什么,

只有在所有情况下都成立且成立的情况都是同一个值,既可以确定,这里用一个yes数组去统计确定的情况。

然后people存储假设的情况方便递归,now存储的假设成立的情况。

如果有情况成立但是没有确定的就是第二种情况。有确定的情况输出就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<climits>
#define LL long long
using namespace std;
const int N=201320;
const LL mod=530600414;
int birth[10],yes[10],people[10],now[10];
int n;
struct say
{
    char talk;
    char name;
    int bo;
    int sex;
};
int ok;
int writer(string s)
{
    if(s=="divine.")
        return 3;
    if(s=="human.")
        return 2;
    if(s=="evil.")
        return 1;
    if(s=="day.")
        return 5;
    if(s=="night.")
        return 4;
    if(s=="lying.")
        return -1;
}
void read(say *tem)
{
    for(int i=0;i<n;i++)
    {
        string s;
        cin>>s;
        tem[i].talk=s[0];
        int id=s[0]-'A'+1;
        birth[id]=1;
        cin>>s;
        if(s=="It")
        {
            tem[i].name='T';
        }
        else if(s=="I")
            tem[i].name=tem[i].talk;
        else
        {
            tem[i].name=s[0];
            int id=s[0]-'A'+1;
            birth[id]=1;
        }
        cin>>s;
        cin>>s;
        if(s=="not")
        {
            tem[i].bo=0;
            cin>>s;
            tem[i].sex=writer(s);
        }
        else
        {
            tem[i].bo=1;
            tem[i].sex=writer(s);
        }
    }
}
bool true_say(say f)
{
    if(f.sex!=-1)
    {
        if(f.name=='T')
        {
            if(f.bo&&f.sex-4==people[0])
                return 1;
            else if(!f.bo&&f.sex-4!=people[0])
                return 1;
            else
                return 0;
        }
        else
        {
            int id=f.name-'A'+1;
            if(f.bo&&f.sex==people[id])
                return 1;
            else if(!f.bo&&f.sex!=people[id])
                return 1;
            else
                return 0;
        }
    }
    else
    {
        int id=f.name-'A'+1;
        if(people[id]==3&&!f.bo)
            return 1;
        else if(people[id]==1&&f.bo)
            return 1;
        else if(people[id]==2)
        {
            if(people[0]&&!f.bo)
                return 1;
            else if(!people[0]&&f.bo)
                return 1;
            else
                return 0;
        }
        else
            return 0;
    }
}
bool judge(say tem[])
{
    for(int i=0;i<n;i++)
    {
        int id=tem[i].talk-'A'+1;
        if(people[id]==3)
        {
            if(true_say(tem[i]))
                continue;
            else
                return 0;
        }
        else if(people[id]==2)
        {
            if(people[0]&&true_say(tem[i]))
                continue;
            if(!people[0]&&!true_say(tem[i]))
                continue;
            else
                return 0;
        }
        else if(people[id]==1)
        {
            if(!true_say(tem[i]))
                continue;
            else
                return 0;
        }
    }
    return 1;
}
void thesome()
{
    for(int i=0;i<6;i++)
        if(now[i]!=people[i])
        yes[i]=1;
}
void build(int k,say *tem)
{
    if(k<6)
    {
        if(birth[k])
        {
            for(people[k]=1;people[k]<4;people[k]++)
                build(k+1,tem);
        }
        else
            build(k+1,tem);
    }
    else if(judge(tem))
    {
        if(ok>=1)
            thesome();
        else
            for(int i=0;i<6;i++)
            now[i]=people[i];
        ok++;
    }
}
int main()
{
    int cas=0;
    while(cin>>n&&n)
    {
        memset(birth,0,sizeof(birth));
        memset(now,0,sizeof(now));
        memset(yes,0,sizeof(yes));
        memset(people,0,sizeof(people));
        ok=0;
        say tem[60];
        read(tem);
        for(people[0]=0;people[0]<2;people[0]++)
            build(1,tem);
        cout<<"Conversation #"<<++cas<<endl;
        if(!ok)
            cout<<"This is impossible."<<endl;
        else
        {
            int oi=0;
            for(int i=1;i<6;i++)
            {
                if(!yes[i]&&now[i])
                {
                    oi++;
                    char c='A'+i-1;
                    if(now[i]==3)
                     cout<<c<<" is divine."<<endl;
                     else if(now[i]==2)
                            cout<<c<<" is human."<<endl;
                     else if(now[i]==1)
                        cout<<c<<" is evil."<<endl;
                }
            }
            if(!oi&&yes[0])
                cout<<"No facts are deducible."<<endl;
            if(now[0]&&!yes[0])
                cout<<"It is day."<<endl;
            else if(!yes[0])
                cout<<"It is night."<<endl;
        }
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值