zoukankan      html  css  js  c++  java
  • codeforces 887B 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.

    思路:

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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 int weishu(int num) {
     6     if(num>=1&&num<=9) return 1;
     7     if(num>=10&&num<=99) return 2;
     8     if(num>=100&&num<=999) return 3;
     9     return 4;
    10 }
    11 bool exist(int ans[][10], int rec[], int len) {
    12     if(len==1) {
    13         int a=rec[1];
    14         if(ans[1][a]||ans[2][a]||ans[3][a]) return true;
    15     } else if(len==2) {
    16         int a=rec[1],b=rec[2];
    17         if((ans[1][a]&&ans[2][b])
    18             ||(ans[1][a]&&ans[3][b])
    19             ||(ans[2][a]&&ans[3][b])
    20             ||(ans[3][a]&&ans[2][b])
    21             ||(ans[3][a]&&ans[1][b])
    22             ||(ans[2][a]&&ans[1][b]))
    23         return true;
    24     } else if(len==3) {
    25         int a=rec[1],b=rec[2],c=rec[3];
    26         if((ans[1][a]&&ans[2][b]&&ans[3][c])
    27             ||(ans[1][a]&&ans[3][b]&&ans[2][c])
    28             ||(ans[2][a]&&ans[3][b]&&ans[1][c])
    29             ||(ans[2][a]&&ans[1][b]&&ans[3][c])
    30             ||(ans[3][a]&&ans[1][b]&&ans[2][c])
    31             ||(ans[3][a]&&ans[2][b]&&ans[1][c]))
    32         return true;
    33     }
    34     return false;
    35 }
    36 int main(){
    37     int ans[4][10],n,num,bns[4][10],rec[4];
    38     memset(ans,0,sizeof(ans));
    39     /*read*/
    40     scanf("%d",&n);
    41     for(int i=1;i<=n;++i) {
    42         int flag[10]={0};
    43         for(int j=0;j<6;++j) {
    44             scanf("%d",&num);
    45             if(flag[num]==0) {
    46                 ans[i][num]++;
    47                 flag[num]=1;
    48             }
    49         }
    50     }
    51     /*slove*/
    52     int result=0;
    53     for(int i=1;i<=1000;++i) {
    54         if(i==1000) {
    55             result=999;
    56             break;
    57         }
    58         int len=weishu(i);
    59         int temp=i;
    60         memcpy(bns,ans,sizeof(ans));
    61         memset(rec,-1,sizeof(rec));
    62         for(int j=1;j<=len;++j) {
    63             int r=i%10;
    64             rec[j]=r;
    65             i=i/10;
    66         }
    67         i=temp;
    68         if(!exist(ans,rec,len)) {
    69             result=i-1;
    70             break;
    71         }
    72     }
    73     printf("%d
    ",result);
    74     return 0;
    75 }
    View Code

     优化暴力模拟,不可能出现比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大的数字。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int main() {
     4     int n,ans[4][7],vis[100];
     5     memset(vis,0,sizeof(vis));
     6     scanf("%d",&n);
     7     for(int i=1;i<=n;++i) {
     8         for(int j=1;j<=6;++j) {
     9             scanf("%d",&ans[i][j]);
    10         }
    11     }
    12     for(int i=1;i<=n;++i) {
    13         for(int j=1;j<=6;++j) {
    14             vis[ans[i][j]]=1;
    15             for(int k=1;k<=n;++k) {
    16                 if(k==i) continue;
    17                 for(int l=1;l<=6;++l) {
    18                     vis[ans[i][j]*10+ans[k][l]]=1;
    19                 }
    20             }
    21         }
    22     }
    23     for(int i=1;i<=99;++i) {
    24         if(!vis[i]) {
    25             printf("%d
    ",i-1);
    26             break;
    27         }
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    Django models中的null和blank的区别
    微服务
    幂等性
    restful规范
    related_name
    数据库 引擎,数据类型,约束
    数据库 基本操作
    python 常见算法
    python if,循环的练习
    python数据类型、if判断语句
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7783169.html
Copyright © 2011-2022 走看看