B - Bulls and Cows CodeForces - 63C(构造,模拟)

2024-04-16 03:32

本文主要是介绍B - Bulls and Cows CodeForces - 63C(构造,模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Input
The first input line contains an integer n (1 ≤ n ≤ 10) which represents the number of already made guesses. Then follow n lines in the form of “ai bi ci”, where ai is the i-th experimental number, bi is the number of bulls, ci is the number of cows (1 ≤ i ≤ n, 0 ≤ bi, ci, bi + ci ≤ 4). The experimental numbers are correct, i.e., each of them contains exactly four digits, in each of them all the four digits are different, and there can be a leading zero. All the experimental numbers are different. As the guesser hasn’t guessed the number yet, the answer “4 bulls 0 cows” is not present.

Output
If the input data is enough to determine the sought number, print the number with four digits on a single line. If it has less than four digits, add leading zero. If the data is not enough, print “Need more data” without the quotes. If the thinker happens to have made a mistake in his replies, print “Incorrect data” without the quotes.

Examples
Input
2
1263 1 2
8103 2 1
Output
Need more data
Input
2
1234 2 2
1256 0 2
Output
2134
Input
2
0123 1 1
4567 1 2
Output
Incorrect data

题意: 要你猜一个数字,给你的n个数字和匹配的结果。
思路: 基本思路是模拟,只要模拟不写出问题就没有问题。因为n的范围相当小,所以暴力是没问题的。所以考虑优化,题目给的是相同位置的数字个数和不相同位置的相同数字个数,或许可以从这里考虑一些优化方案。如果n比较小的话,可以把n个数字合在一起,然后状态压缩枚举合法状态;相同位置相同数字的状态不难枚举,但是如果是不同位置的相同数字呢?或许可以考虑状态再多开一个进制,一个进制记录相同位置的相同数字,另外的进制记录不同位置的相同数字。虽然多开了一维,但是状态被压缩的更多了,差别应该也不大。
下面的代码是暴力代码,究极暴力啊。

#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
#include<algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;int a[200][200],n;
vector<pair<int,int> >query;
vector<int>ans;
bool judge(int x,int y,int z,int k)
{int b[10];b[1] = x;b[2] = y;b[3] = z;b[4] = k;for(int i = 1;i <= n;i++){int tmp1 = 0,tmp2 = 0;for(int j = 1;j <= 4;j++){if(a[i][j] == b[j])tmp1++;}for(int j = 1;j <= 4;j++){for(int q = 1;q <= 4;q++){if(j == q)continue;if(a[i][j] == b[q])tmp2++;}}if(tmp1 == query[i - 1].first && tmp2 == query[i - 1].second){continue;}else{return false;}}return true;
}int main()
{scanf("%d",&n);for(int i = 1;i <= n;i++){for(int j = 1;j <= 4;j++){scanf("%1d",&a[i][j]);}int x,y;scanf("%d%d",&x,&y);query.push_back(make_pair(x,y));}for(int i = 0;i <= 9;i++){for(int j = 0;j <= 9;j++){if(i == j)continue;for(int k = 0;k <= 9;k++){if(k == i || k == j)continue;for(int q = 0;q <= 9;q++){if(q == i || q == j || q == k)continue;if(judge(i,j,k,q)){ans.push_back(i * 1000 + j * 100 + k * 10 + q);}}}}}if((int)ans.size() == 1){printf("%04d\n",ans[0]);}else if((int)ans.size() >= 2){printf("Need more data\n");}else{printf("Incorrect data");}return 0;
}

这篇关于B - Bulls and Cows CodeForces - 63C(构造,模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907697

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

hdu4431麻将模拟

给13张牌。问增加哪些牌可以胡牌。 胡牌有以下几种情况: 1、一个对子 + 4组 3个相同的牌或者顺子。 2、7个不同的对子。 3、13幺 贪心的思想: 对于某张牌>=3个,先减去3个相同,再组合顺子。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOExcepti

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点