【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 ...
随机推荐
- JavaScript Patterns 2.6 switch Pattern
Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Ind ...
- XMLHttpRequest 对象-回调函数
回调函数是一种以参数形式传递给另一个函数的函数. 如果您的网站上存在多个 AJAX 任务,那么您应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数. ...
- 网站防止用户复制的js方法
<script type="text/javascript">function stop() {return false;}document.oncontextmenu ...
- bzoj2431: [HAOI2009]逆序对数列(前缀和优化dp)
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2312 Solved: 1330[Submit][Stat ...
- Appium + python -yaml配置文件
在线安装:pip install yaml import yamlimport os # 获取当前脚本所在文件夹路径curpath = os.path.dirname(os.path.realpath ...
- vue---思维导图
持续更新啦啦啦啦
- 卸载Mysql connect 6.9.9
我们在卸载MySQL的时候,会发现有一个名为“Connector Net X.X.X”(如:Connector Net 6.9.9)软件总是卸载不成功,下面我们来看看解决方法:1. 在C盘的目录下,有 ...
- IIS: 响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法
以前好好的项目,突然出现了这个问题.一顿google后无果,有人说是程序池的原因,有人说是安全配置的原因,照着网上方法试了多次,还是没有解决.如下图: 突然发现了一片文章 (https://www.j ...
- JavaScript:常用总结
● 语法要点 ● 引用类型 ● BOM/DOM ● 事件要点 一.语法要点 1.在声明变量时,如果省去了var关键字,那么这个变量为全局变量. 2.数据类型有5种原始类型和9种引用类型: 原始类型中: ...
- java 多线程并发系列之 生产者消费者模式的两种实现
在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度. 为什么要使用生产者和消费者模式 在线程世界里,生产者就是生产数据 ...