B. Cubes for Masha
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Absent-minded Masha got set of n cubes for her birthday.

At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.

To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.

The number can't contain leading zeros. It's not required to use all cubes to build a number.

Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.

Input

In first line integer n is given (1 ≤ n ≤ 3) — the number of cubes, Masha got for her birthday.

Each of next n lines contains 6 integers aij (0 ≤ aij ≤ 9) — number on j-th face of i-th cube.

Output

Print single integer — maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.

Examples
input
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7
output
87
input
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9
output
98
Note

In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.

思路:

暴力模拟,一个数字里面每个骰子有且至多能用一次

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int weishu(int num) {
if(num>=&&num<=) return ;
if(num>=&&num<=) return ;
if(num>=&&num<=) return ;
return ;
}
bool exist(int ans[][], int rec[], int len) {
if(len==) {
int a=rec[];
if(ans[][a]||ans[][a]||ans[][a]) return true;
} else if(len==) {
int a=rec[],b=rec[];
if((ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b]))
return true;
} else if(len==) {
int a=rec[],b=rec[],c=rec[];
if((ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c]))
return true;
}
return false;
}
int main(){
int ans[][],n,num,bns[][],rec[];
memset(ans,,sizeof(ans));
/*read*/
scanf("%d",&n);
for(int i=;i<=n;++i) {
int flag[]={};
for(int j=;j<;++j) {
scanf("%d",&num);
if(flag[num]==) {
ans[i][num]++;
flag[num]=;
}
}
}
/*slove*/
int result=;
for(int i=;i<=;++i) {
if(i==) {
result=;
break;
}
int len=weishu(i);
int temp=i;
memcpy(bns,ans,sizeof(ans));
memset(rec,-,sizeof(rec));
for(int j=;j<=len;++j) {
int r=i%;
rec[j]=r;
i=i/;
}
i=temp;
if(!exist(ans,rec,len)) {
result=i-;
break;
}
}
printf("%d\n",result);
return ;
}

优化暴力模拟,不可能出现比99大的数字,因为如果要出现比99大的数字,那么需要有1 1,2 2,3 3,4 4,5 5,6 6,7 7,8 8,9 9这样的话已经有18个数字,即使三个骰子也只是刚好18个数字。其中必不可少的是数字0,加上就是19。所以不可能出现比99大的数字。

 #include <bits/stdc++.h>
using namespace std;
int main() {
int n,ans[][],vis[];
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<=n;++i) {
for(int j=;j<=;++j) {
scanf("%d",&ans[i][j]);
}
}
for(int i=;i<=n;++i) {
for(int j=;j<=;++j) {
vis[ans[i][j]]=;
for(int k=;k<=n;++k) {
if(k==i) continue;
for(int l=;l<=;++l) {
vis[ans[i][j]*+ans[k][l]]=;
}
}
}
}
for(int i=;i<=;++i) {
if(!vis[i]) {
printf("%d\n",i-);
break;
}
}
return ;
}

codeforces 887B Cubes for Masha 两种暴力的更多相关文章

  1. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  2. Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]

    B. Our Tanya is Crying Out Loud time limit per test 1 second memory limit per test 256 megabytes inp ...

  3. Codeforces Beta Round #13 E. Holes 分块暴力

    E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...

  4. Sql Server 聚集索引扫描 Scan Direction的两种方式------FORWARD 和 BACKWARD

    最近发现一个分页查询存储过程中的的一个SQL语句,当聚集索引列的排序方式不同的时候,效率差别达到数十倍,让我感到非常吃惊 由此引发出来分页查询的情况下对大表做Clustered Scan的时候, 不同 ...

  5. Codeforces Round #369 (Div. 2) A B 暴力 模拟

    A. Bus to Udayland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #329 (Div. 2) A. 2Char 暴力

    A. 2Char Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/problem/A De ...

  7. linux尝试登录失败后锁定用户账户的两种方法

    linux尝试登录失败后锁定用户账户的两种方法 更新时间:2017年06月23日 08:44:31   作者:Carey    我要评论   这篇文章主要给大家分享了linux尝试登录失败后锁定用户账 ...

  8. 寒假集训——搜索 D - Cubes for Masha

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...

  9. ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板)

    ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板) 题意 题意:给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000 这 ...

随机推荐

  1. Myeclipse常见快捷键及配置

    0. 快捷键 ================================================================================ 编辑: Ctrl+Shi ...

  2. IOS 中的JS

     文章摘自: http://www.cocoachina.com/ios/20150127/11037.html  JSContext/JSValue JSContext 即JavaScript代码的 ...

  3. 【Salvation】——关卡功能&数据库基础实现

    写在前面:项目的关卡功能和数据库基础实现是小组其他成员实现的部分,这里作为学习总结.关卡功能块使用C#语言编写脚本,在Unity3D游戏引擎的环境中实现,数据库功能块使用PHP作为服务端获取MySQL ...

  4. spark join操作解读

    本文主要介绍spark join相关操作,Java描述. 讲述三个方法spark join,left-outer-join,right-outer-join 我们以实例来进行说明.我的实现步骤记录如下 ...

  5. JS深层继承

    我们在书写JS的时候常常被一种现象困扰 let jsonA = { a1: { b1:1; }, }; let jsonB = jsonA; jsonB.a1.b1 = 2; console.log( ...

  6. shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~]# cat /server/scripts/show2.sh #!/bin/ ...

  7. Android 开发笔记___RadioButton

    horizontal <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" and ...

  8. 运维必须掌握的150个Linux命令

    线上查询及帮助命令(1个)man 目录操作命令(6个)ls tree pwd mkdir rmdir cd 文件操作命令(7个)touch cp mv rm ln find rename 文件查看及处 ...

  9. tomcat配置虚拟路径保存、访问图片

    一些项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问.这样当我们系统需要进行升级,进行全 ...

  10. 《天书夜读:从汇编语言到windows内核编程》二 C语言的流程与处理

    1) Debug与Release的区别:前者称调试版,后者称发行版.调试版基本不优化,而发行版会经过编译器的极致优化,往往与优化前的高级语言执行流程会大相径庭,但是实现的功能是等价的. 2) 如下fo ...