B - Cubes for Masha題解

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

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.git

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.ui

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

Input

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

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

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.blog

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.ci

題目大意:最多有三個六面體,每一個六面體有6個數字,能夠用任意個六面體組成數字,可是一次每一個六面體只能用一次,問你能組成最大的連續數字是多少?例如樣例1:能夠組成1,2,3,4,。。87input

題解: 由於最大隻有三個六面體,最多有999種狀況,dfs遍歷每種狀況,存入v【值】= 1,不然默認 v【值】 = 0;最後從1到1000遍歷v數組,查找第一個斷點則是答案。

 1 /* ***********************************************
 2 Author        :wsw
 3 Created Time  :2017/11/7 17:27:24
 4 TASK          :cf_R444_B.cpp
 5 LANG          :C++
 6 ************************************************ */
 7 #include <iostream>
 8 #include <cstdio>
 9 #include <cstring>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <cmath>
17 #include <cstdlib>
18 #include <ctime>
19 using namespace std;
20 int n;
21 int num[3][6];
22 int value[1000];//存值
23 int vis[3];//記錄是用過
24 void dfs(int ant,int no){
25     value[no] = 1;
26     if(ant>n) return ;
27     for(int i = 0;i<n;i++){
28         if(vis[i]) continue;
29         for(int j = 0;j<6;j++){
30             vis[i] = 1;
31             dfs(ant+1,no*10+num[i][j]);
32             vis[i] = 0;
33         }
34     }
35 }
36 int main()
37 {
38     cin >> n;
39     for(int i = 0;i<n;i++){
40         for(int j = 0; j < 6;j++){
41             cin >> num[i][j];
42         }
43     }
44     dfs(0,0);
45     for(int i = 1;i<1000;i++){
46         if(!value[i]){
47             cout << i-1 << endl;
48             break;
49         }
50     }
51     return 0;
52 }

人生路也許有不少條,但願你能走完屬於你的那一條。

相關文章
相關標籤/搜索