【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 ...
随机推荐
- C#的内存管理知识 .
本章介绍内存管理和内存访问的各个方面.尽管运行库负责为程序员处理大部分内存管理工作,但程序员仍必须理解内存管理的工作原理,了解如何处理未托管的资源. 如果很好地理解了内存管理和C#提供的指针功能,也就 ...
- 【BeijingWc 2008】 秦腾与教学评估
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1271 [算法] 二分 [代码] #include<bits/stdc++.h& ...
- 解决 EF where<T>(func) 查询的一个性能问题
前两年帮朋友 做了个网吧管理软件,采用动软的三层架构 sql语句生成的.最近因功能变更 要改动,而我这段正在做asp.net mvc +ef+autofac的一个电商网站.索性 就把原来的底层全重新了 ...
- nodejs在windows下的安装
Windowv 上安装Node.js Windows 安装包(.msi) : 32 位安装包下载地址 : http://nodejs.org/dist/v0.10.26/node-v0.10.26-x ...
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
- Activity启动模式(GIF 动态演示)
本文首发在我的个人微信公众号:Android开发圈 引言 关于Activity的启动模式是面试高频问题,在平时开发中,作用也不小,所以还是很有必要搞懂这一块的知识.其实之前也有写过这个主题的文章,但是 ...
- jmeter关联、下载文件、简单压测
关联 一.什么是关联 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 简单地说就是在测试过程中有些数据的值会经常发生变化,要获取并使用这些数据,把这个动态的信 ...
- ACM_lowbit
lowbit Time Limit: 2000/1000ms (Java/Others) Problem Description: long long ans = 0; for(int i = 1; ...
- [转]linux之pr命令
转自:http://www.bitscn.com/plus/view.php?aid=6638 本文介绍如何使用Linux的pr命令将大文件分割成多个页面进行打印,并在每个页面上加上标题. Linux ...
- 00-SQLite的SQL语法
SQLite的SQL语法 SQLite库可以解析大部分标准SQL语言.但它也省去了一些特性并且加入了一些自己的新特性.这篇文档就是试图描述那些SQLite支持/不支持的SQL语法的.查看关键字列表. ...