AOJ 2084 Hit and Blow

来源:互联网 发布:淘宝女装店铺介绍范文 编辑:程序博客网 时间:2024/06/07 21:15

非常有趣的模拟题, 题意是曾经很火的小游戏: 猜数字

首先出题人决定了一个秘密可带前导0的四位数, 然后每轮挑战者也提出一个可有前导0的四位数字, 就会得到一个结果 X hit Y blow

hit 表示数字对位置也对, blow表示数字对但位置不对

比如秘密数字为1579 挑战者提出2507 就会得到1 hit 1 blow的结果

现在给定一个猜测数次的结果, 询问挑战者能否依靠逻辑在下两次尝试中猜出正确答案

如果不能猜的正确答案, 输出????, 否则 如果只有唯一解, 输出答案, 如果不是唯一解, 输出最小的critical number, 关键数的定义如下:

critical numbers mean those such that, after being given the number of hits and blows for them on the next attempt, the codebreaker can determine the secret number uniquely.(意即为解出答案下一次需要尝试的数)

注意题意要求每位数字都不同

暴力0123至9876即可

具体解法可见代码>>>


/*author: birdstorm*/#include <iostream>#include <cstdio>#include <cstring>#include <cctype>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <climits>#define MAXN 205#define N 105#define inf 1.0e20#define eps 1.0e-10#define MOD 1000000007#define For(i,m,n) for(int i=(m);i<(n);i++)#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)#define rep(i,m,n) for(int i=(m);i<=(n);i++)#define LL long longusing namespace std;vector<int> ans, all, temp;int hit, blow, att;int h(int a, int b) //计算hit数{    int x=0;    For(i,0,4) x+=(a%10==b%10),a/=10,b/=10;    return x;}int b(int a, int b) //计算blow数{    int x=0, aa=a, bb;    For(i,0,4){        bb=b;        For(j,0,4){            if(i!=j&&aa%10==bb%10) x++;            bb/=10;        }        aa/=10;    }    return x;}bool differ(int x) //各位数字不同{    bool vis[10]={false};    For(i,0,4){        if(vis[x%10]) return false;        else vis[x%10]=true;        x/=10;    }    return true;}int main(){    int T, n;    rep(i,123,9876) if(differ(i)) all.push_back(i); //全集合    while(scanf("%d",&n),n){        ans=all;        rep(i,1,n){            scanf("%d%d%d",&att,&hit,&blow);            temp.clear();            vecfor(iter,ans) if(h(*iter,att)==hit&&b(*iter,att)==blow) temp.push_back(*iter);            ans=temp; //每次猜测都将缩减答案集合            //vecfor(iter,ans) printf("%6d",*iter);        }        if(ans.empty()) puts("????");        else if(ans.size()==1) printf("%04d\n",ans[0]);        else if(ans.size()<=24){ //答案数超过24即表明不能确定这个四位数的各个数字, 明显不能解出答案            bool hb[5][5];            vecfor(iter1,all){                memset(hb,false,sizeof hb);                vecfor(iter2,ans){                    int th=h(*iter1,*iter2), tb=b(*iter1,*iter2);                    if(hb[th][tb]) goto next;                    else hb[th][tb]=true;                }                printf("%04d\n",*iter1);                goto end;                next:;            }            puts("????");            end:;        }        else puts("????");    }    return 0;}



0 0
原创粉丝点击