zoukankan      html  css  js  c++  java
  • 【Codeforces 63C】Bulls and Cows

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

    给你一个长度为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");
            }
        }
    }
    
    
  • 相关阅读:
    线程,协程
    python魔法方法详解
    Sorted方法排序用法
    time模块
    Haroopad安装与配置: Linux系统下最好用的Markdown编辑器
    C++ Primer第五版答案
    Ubuntu14.04安装有道词典(openyoudao)
    Ubuntu14.04下Sublime Text 3解决无法输入中文
    OpenLTE安装教程
    GNU Radio: Overview of the GNU Radio Scheduler
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10325543.html
Copyright © 2011-2022 走看看