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

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
int num[4][10],n;
int vis[1100],exa[10]; void dfs(int ceng,int s)
{
vis[s]=1;
if(ceng>=n) return;
for(int i=1;i<=n;i++)
{
if(exa[i]) continue;
for(int j=1;j<=6;j++)
{
exa[i]=1;
dfs(ceng+1,s*10+num[i][j]);
exa[i]=0;
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
memset(exa,0,sizeof(exa));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=6;j++)
{
scanf("%d",&num[i][j]);
}
}
dfs(0,0);
int mark;
for(int i=1;i<1000;i++)
{
if(!vis[i])
{
mark=i-1;
break;
}
}
printf("%d\n",mark);
}
return 0;
}

  

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.

题目:D - Cubes for Masha
思路:
因为最多为三个魔方,意思是数字最大为999
所以可以采用枚举遍历的方法。
简单来说,就是用搜索把三个魔法可以组成的所有的数全部搜出来,然后找到最近的断点。
具体:
用一个二维数组,或者三个一维数组储存魔方上的六个数。
用vis数组来表示这个数是否存在,1存在,0不存在。
搜索里面,注意一个魔方不能同时用上面的两个数,所以要用东西标记这个魔方是否用过了

上一篇:Spring学习总结(14)——Spring10种常见异常解决方法


下一篇:Orchard之模版开发