Codeforces Round #444 (Div. 2) B. Cubes for Masha 暴力、枚举

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.


Source

Codeforces Round #444 (Div. 2)

My Solution

题意:有n(1<= n <= 3)个骰子(每面标着数字0~9),要求找出最大的数x,满足1~x之间所有的数都可以用这最多n个骰子的正面表示出来。不能旋转,即不能用9表示6,反正亦然,且不要求所有的骰子都用上)。

暴力、枚举

首先用一个标记数组f,先全标记为false,之后把出现过的数都标记为true。

当n == 1的时候,只有枚举每一面,并标记即可。

当n == 2的时候,要枚举只用到1个骰子的情况和要用到2个骰子的情况,同时手动枚举全排列。

当n == 3的时候,要枚举只用到1个或者2个骰子的情况和要用到3个骰子的情况,同时手动枚举全排列

详情请见代码。

时间复杂度 O(1000)

空间复杂度 O(1000)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e3 + 8;

bool f[MAXN];
int v[6][6];

int main()
{
    #ifdef LOCAL
    freopen("b.txt", "r", stdin);
    //freopen("b.out", "w", stdout);
    int T = 4;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int n, i, j, k, ans = 0;
    cin >> n;
    for(i = 0; i < n; i++){
        for(j = 0; j < 6; j++){
            cin >> v[i][j];
        }
    }
    if(n == 1){
        for(j = 0; j < 6; j++){
            f[v[0][j]] = true;
        }
    }
    else if(n == 2){
        for(j = 0; j < 6; j++){
            f[v[0][j]] = true;
        }
        for(j = 0; j < 6; j++){
            f[v[1][j]] = true;
        }
        for(i = 0; i < 6; i++){
            for(j = 0; j < 6; j++){
                f[v[0][i]*10 + v[1][j]] = true;
                f[v[1][i]*10 + v[0][j]] = true;
            }
        }

    }
    else{
        for(j = 0; j < 6; j++){
            f[v[0][j]] = true;
        }
        for(j = 0; j < 6; j++){
            f[v[1][j]] = true;
        }
        for(j = 0; j < 6; j++){
            f[v[2][j]] = true;
        }
        for(i = 0; i < 6; i++){
            for(j = 0; j < 6; j++){
                for(k = 0; k < 6; k++){
                    f[v[0][i]*100 + v[1][j]*10 + v[2][k]] = true;
                    f[v[1][i]*100 + v[0][j]*10 + v[2][k]] = true;

                    f[v[0][i]*100 + v[2][j]*10 + v[1][k]] = true;
                    f[v[2][i]*100 + v[0][j]*10 + v[1][k]] = true;

                    f[v[2][i]*100 + v[1][j]*10 + v[0][k]] = true;
                    f[v[1][i]*100 + v[2][j]*10 + v[0][k]] = true;
                }
            }
        }
        for(i = 0; i < 6; i++){
            for(j = 0; j < 6; j++){
                f[v[0][i]*10 + v[1][j]] = true;
                f[v[1][i]*10 + v[0][j]] = true;

                f[v[0][i]*10 + v[2][j]] = true;
                f[v[2][i]*10 + v[0][j]] = true;

                f[v[2][i]*10 + v[1][j]] = true;
                f[v[1][i]*10 + v[2][j]] = true;
            }
        }

    }
    for(i = 1; i < MAXN; i++){
        if(!f[i]){
            break;
        }
        ans = i;
    }
    cout << ans << endl;


    #ifdef LOCAL
    memset(f, false, sizeof f);
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}

  Thank you!

                                                                                                                                             ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值