暴力

HDU1172

主要是第二个是深搜的思想,值得学习,第一个纯暴力,一定要记住,暴力总比没思路强!!!
Description

猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么。每猜一个数,计算机都会告诉玩家猜对几个数字,其中有几个数字在正确的位置上。
比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。
现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。
Input

输入数据有多组。每组的第一行为一个正整数N(1<=N<=100),表示在这段对话中共有N次问答。在接下来的N行中,每行三个整数A,B,C。gameboy猜这个四位数为A,然后计算机回答猜对了B个数字,其中C个在正确的位置上。当N=0时,输入数据结束。

#include<stdio.h>
#include<cstring>
using namespace std;
const int MAXN=100;
struct Node
{
    int a,b,c;
} node[MAXN];

bool Judge(int k,int number)
{
    int num1[5],num2[5];
    bool mark[5];
    for(int i=1; i<=4; i++)mark[i]=false;

    num1[1]=node[k].a/1000;
    num1[2]=(node[k].a%1000)/100;
    num1[3]=(node[k].a%100)/10;
    num1[4]=(node[k].a%10);

    num2[1]=number/1000;
    num2[2]=(number%1000)/100;
    num2[3]=(number%100)/10;
    num2[4]=(number%10);

    int count=0;
    for(int i=1; i<=4; i++)//相同位置相同的数
    {
        if(num1[i]==num2[i])count++;
    }
    if(count!=node[k].c)return false;//如果个数不符就返回错

    count=0;
    for(int i=1; i<=4; i++)//相同数字的个数
    {
        for(int j=1; j<=4; j++)
        {
            if(num1[i]==num2[j]&&!mark[j])
            {
                mark[j]=true;
                count++;
                break;
            }
        }
    }
    if(count!=node[k].b)return false;

    return true;
}



int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&node[i].a,&node[i].b,&node[i].c);
        }
        int count=0,result;
        bool flag=true;
        for(int num=1000; num<=9999; num++)//暴力枚举
        {
            for(int i=1; i<=n; i++)
            {
                flag=Judge(i,num);
                if(!flag)break;
            }
            if(flag)
            {
                count++;
                result=num;
            }
        }
        if(count==1)
        {
            printf("%d\n",result);
        }
        else
            printf("Not sure\n");
    }
    return 0;
}


 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#define MIN( x, y ) (x) < (y) ? (x) : (y)
using namespace std;

struct Node
{
    char num[10];
    int right, pos;    
}n[105];

void cmp( char *s, char *c, int &right, int &pos )
{
    right = pos = 0;
    int shash[15] = {0};
    int chash[15] = {0}; 
    for( int i = 0; i < 4; ++i )
    {
        if( s[i] == c[i] )
            pos++;
    }
    for( int i = 0; i < 4; ++i )
    {
        shash[ s[i] ]++;
        chash[ c[i] ]++; 
    }
    for( int i = 0; i < 10; ++i )
    {
        right += MIN( shash[i], chash[i] ); 
    }
}

void getnext( char *s )
{
    for( int i = 3; i >= 0; --i )
    {
        if( s[i] < 9 ) 
        {
            s[i]++;   
            return;
        }
        else
        {
            s[i] = 0;
        }
    }    
}

void DFS( bool &ans, int &num, int N )
{
    int right, pos, flag, cnt = 0;
    char t[4] = {0};
    for( int i = 0; i < 10000; ++i )
    {
        flag = 0;
        for( int i = 0; i < N; ++i )
        {
            cmp( t, n[i].num, right, pos );
            if( right == n[i].right && pos == n[i].pos )
                continue;
            else
            {
                flag = 1;
                break;
            }
        }    
        if( !flag )
        {
            ans = true;
            cnt++;
            num = t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];
            if( cnt > 1 )
            {
                ans = false;
                return;    
            }
        }
        getnext( t );
    }
}

int main()
{
    int N;
    while( scanf( "%d", &N ), N )
    {
        int num = 0;
        for( int i = 0; i < N; ++i )
        {
            scanf( "%s %d %d", n[i].num, &n[i].right, &n[i].pos );        
            for( int j = 0; j < 4; ++j )
                n[i].num[j] -= '0';
        }
        bool ans = false;
        DFS( ans, num, N );
        if( ans ) 
            printf( "%d\n", num );
        else
            puts( "Not sure" );
    };
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值