码迷,mamicode.com
首页 > 其他好文 > 详细

【Codeforces 63C】Bulls and Cows

时间:2019-01-27 10:54:22      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:class   correct   final   ble   href   个数   def   .com   check   

【链接】 我是链接,点我呀:)
【题意】


给你一个长度为4的数字序列(每个数字都在0~9之间,且不重复出现)
现在让你猜这个长度为4的序列是什么.
猜了之后对方会告诉有几个数字是位置和数字都正确的(猜的数字序列有顺序)
以及有几个数字是数字出现了但是位置不正确.
即给你两个反馈。
现在给你n个猜的过程(猜的4个数字以及对应的两个反馈)
问你是否能唯一的确定一个4位数字的答案

【题解】


列举出来最后的4位数的所有可能。
对于每一种可能 将其对这n个猜的过程验证一遍》
如果验证通过,那么递增sum
最后如果sum==1则说明存在唯一的一个可能.
sum==0说明没有任何一个可能,则数据有错
sum>1则说明需要更多的数据才能确定是哪一个.

【代码】

import java.io.*;
import java.util.*;

public class Main {

    static int N = (int)10;
    static String s[];
    static int b[],c[],n,ans[],sum,final_ans[];
    static boolean bo[];
    static boolean flag[];

    public static boolean check(){
        for (int i = 1;i <= n;i++){
            for (int j = 0;j <= 9;j++) flag[j] = false;
            int cnt1 = 0,cnt2 = 0;
            for (int j = 0;j < 4;j++)
                if ( (s[i].charAt(j)-'0') == ans[j+1]){
                    cnt1++;
                }else{
                    flag[s[i].charAt(j)-'0'] = true;
                }
            for (int j = 1;j <= 4;j++)
                if (flag[ans[j]]==true){
                    cnt2++;
                }
            if(cnt1==b[i] && cnt2==c[i]){
                continue;
            }else return false;
        }
        return true;
    }

    public static void dfs(int dep){
        if (sum>=2) return;
        if (dep==5){
            if (check()==true){
                for (int i = 1;i <= 4;i++) final_ans[i] = ans[i];
                sum++;
            }
            return;
        }
        for (int i = 0;i <= 9;i++)
            if (bo[i]==false){
                bo[i] = true;
                ans[dep] = i;
                dfs(dep+1);
                bo[i] = false;
            }
    }

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        bo = new boolean[N+10];
        s = new String[N+10];
        b = new int[N+10];c = new int[N+10];
        ans = new int[N+10];
        flag = new boolean[N+10];
        final_ans = new int[N+10];
        sum = 0;

        n = in.nextInt();
        for (int i = 1;i <= n;i++){
            s[i] = in.next();b[i] = in.nextInt();c[i] = in.nextInt();
        }

        dfs(1);
        if (sum==0){
            System.out.println("Incorrect data");
        }else if (sum==1){
            for (int j = 1;j <= 4;j++)
                System.out.print(final_ans[j]);
        }else{
            System.out.print("Need more data");
        }
    }
}

【Codeforces 63C】Bulls and Cows

标签:class   correct   final   ble   href   个数   def   .com   check   

原文地址:https://www.cnblogs.com/AWCXV/p/10325543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!