【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");
}
}
}
【Codeforces 63C】Bulls and Cows的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- ubuntu16.04安装flash player
ubuntu16.04安装flash player sudo apt-get install flashplugin-installer 1 2019: apt-get install browser ...
- Web Tab, Project Properties
https://msdn.microsoft.com/en-us/library/aa983445(v=vs.100).aspx The Web tab of the project Properti ...
- LaTeX 在线编辑器(LaTeX online editors)
eqneditor:有强大的几乎所有常用的数学符号对应的图标形式,便于快速完成latex公式编辑且易于粘贴拷贝. 此外,更为重要的一点是,随着编辑窗口内公式的编辑,会在页面的底部,自动生成其对应的 h ...
- tyvj2054 四叶草魔杖——连通块 & 状压DP
题目:http://www.joyoi.cn/problem/tyvj-2054 把点分成几个连通块,和为0的几个点放在一块,在块内跑最小生成树作为这个块的代价: 然后状压DP,组成全集的最小代价就是 ...
- E20170627-gg
ring n. 戒指,指环; 铃声,钟声; 环形物; 拳击场; vi. 按铃,敲钟; 回响; 成环形; rear n. 后部,背面,背后; 臀部; (舰队或军队的) 后方,后尾,殿后部队; ...
- 0606-工厂模式、单例模式、DBDA的单例和完整功能
工厂模式:只要指定类名,就可以据此获取一个该类的对象. 单例模式:某个类,只允许其“创建”出一个对象. 单例的方法:三私一公(一个私有化对象,一个私有化构造方法,一个私有化克隆方法,一个公共方法返回对 ...
- ubuntu16.04更改源
最近用apt-get安装软件总是提示列表无法全部更新,导致一些软件安装不上,下面我们通过讲/etc/apt/sources.list里为阿里源,实现访问. 第一步: 备份/etc/apt/source ...
- 用python语言写一个简单的计算器
假如我们有这样一个式子: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2 ...
- python值函数名的使用以及闭包,迭代器
一.函数名的运用 函数名就是一个变量名,但它是一个特殊的变量名,是一个后面加括号可以执行函数的变量名. def func(): print("我是一个小小的函数") a = fun ...
- Android Studio 快捷键整理
Alt+回车 导入包,自动修正 Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...