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

【题意】

给你一个长度为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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. Ip获取请求ip

    public class IPAdress { public static bool isIP(string str1) { || str1.Length > ) return false; s ...

  2. P3567 [POI2014]KUR-Couriers 主席树

    这个题比一般主席树还要简单,但是用来练习主席树再好不过了,在这里我再放一下主席树板子. 代码: #include<iostream> #include<cstdio> #inc ...

  3. Luogu4198 楼房重建

    https://zybuluo.com/ysner/note/1124880 题面 带修改的区间维护最大斜率. 题面 解析 用线段树区间维护斜率. 考虑如何向上合并. 左半段一定有贡献. 如果左半段的 ...

  4. 一个不错的jquery插件模版

    pageplugin.js (function ($) { $.PagePlugin = function (obj, opt) { var options = $.extend({}, $.Page ...

  5. vuejs {{}},v-text 和 v-html的区别

    <div id="app"> <p>{{message}}</p> <!-- 输出:<span>通过双括号绑定</spa ...

  6. 322 Coin Change 零钱兑换

    给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...

  7. favourite和favorite的区别

    同一个词,英式和美式的拼写法而已.通常英式英语里的-our-字母组合,到了美式英语里面都成了-or-字母组合,最常见的有英式的 colour,到美式英语成了 color.

  8. Android Activity作为dialog对话框的使用详细介绍

    Activity做为Android应用层四大组件的重要组成部分,它的灵活性.可扩性无论是在应用还是游戏方面都得到了广泛应用,本文主要介绍Activity作为dialog对话框 的使用方法进行说明. 本 ...

  9. 模拟测试—moq:简单一两句

    在Xunit的基础上,说话模拟测试. 假如我们有这样一个控制器里面有这样一个方法,如图 我们在对Bar测试得时候,如果测试未通过,错误有可能来至于Bar,也有可能错误来至于serverde Foo方法 ...

  10. Angular——基本使用

    基本介绍 1.AngularJS是一个框架(诸多类库的集合)以数据和逻辑做为驱动(核心). 2.AngularJS有着诸多特性,最为核心的是:模块化.双向数据绑定.语义化标签.依赖注入等. 模块化 使 ...