codeforces 887B

来源:互联网 发布:可视化编程软件 知乎 编辑:程序博客网 时间:2024/05/17 23:49

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
30 1 2 3 4 56 7 8 9 0 12 3 4 5 6 7
output
87
input
30 1 3 5 6 81 2 4 5 7 82 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.


题意:给出n次6个数(n<=3),用这n个数能组成1到x的每一个数,输出最大的x(可以不全用)

因为数据范围较小,直接暴力求解,简单的搜索


#pragma comment(linker,"/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,mi#define rson rt<<1|1,mi+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;const int mod=1e9+7;int a[5][10],n;bool vis[1010],b[4];void dfs(int p,int s)//p记录当前用了几个数,s表示数的大小,遇到前导0等效那一位数没有用{    vis[s]=1;    if(p>=n)return ;    for(int i=1;i<=n;i++)    {        if(b[i])continue;        for1(j,6)        {            b[i]=1;            dfs(p+1,s*10+a[i][j]);            b[i]=0;        }    }}int main(){    while(~sf("%d",&n))    {        for1(i,n)            for1(j,6)                sf("%d",&a[i][j]);        mem(vis,0);        mem(b,0);        dfs(0,0);        int i;        for(i=1;i<=1000&&vis[i];i++);        pf("%d\n",i-1);    }    return 0;}

原创粉丝点击