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

【题意】

给你一个长度为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. (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)

    (多项式的)因式分解定理(factor theorem)是多项式剩余定理的特殊情况,也就是余项为 0 的情形. 0. 多项式长除法(Polynomial long division) Polynomi ...

  2. codeforces 939F 单调队列优化dp

    F. Cutlet time limit per test 4 seconds memory limit per test 256 megabytes input standard input out ...

  3. bzoj3105 [cqoi2013]新Nim游戏——贪心+线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3105 首先,要先手必胜,就不能取后让剩下的火柴中存在异或和为0的子集,否则对方可以取成异或和 ...

  4. bzoj4872

    期望dp 首先如果k=n的话,那么我们从后往前,只要看到两者的灯就关上,因为如果当前一个灯没关上,那么之后不可能关上,一个灯只能由自己倍数控制,所以这样我们就计算出了需要操作的次数,如果这个次数< ...

  5. 34、JavaScript面向对象(内置构造函数&相关方法|属性|运算符&继承&面向对象)

    一.面向对象 1.1 this的指向问题 要看清楚最终的函数调用者是谁. IIFE也被当做函数直接运行,IIFE的this都是window对象 函数的arguments是类数组对象,比如传入的第0项参 ...

  6. Vue组件库elementUI 在el-row 或 el-col 上使用@click无效失效,

    问题: elementUI 在el-row 或者 el-col 上使用@click失效, 解决: 在click后面加上 .native .要使用@click.native=”handler()”才行, ...

  7. Python 必选参数,默认参数,可变参数,关键字参数和命名关键字参数

    Py的参数还真是多,用起来还是很方便的,这么多参数种类可见它在工程上的实用性还是非常广泛的. 挺有意思的,本文主要参照Liaoxuefeng的Python教程. #必选参数 def quadratic ...

  8. wait、notify、notifyAll实现线程间通信

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  9. Android 自己搭建一个直播系统吧

    服务端用 SRS(Simple Rtmp Server),在这里下载simple-rtmp-server需要Linux系统最好是Ubuntu,装个Ubuntu虚拟机就行了在Linux里,解压缩SRS ...

  10. linux杀掉某个进程的脚本

    https://www.cnblogs.com/zeng1994/p/13a2c5a28e55dd3abc2c75a4fb80371a.html awk的说明: https://www.cnblogs ...