1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6. int num[4][10],n;
  7. int vis[1100],exa[10];
  8.  
  9. void dfs(int ceng,int s)
  10. {
  11. vis[s]=1;
  12. if(ceng>=n) return;
  13. for(int i=1;i<=n;i++)
  14. {
  15. if(exa[i]) continue;
  16. for(int j=1;j<=6;j++)
  17. {
  18. exa[i]=1;
  19. dfs(ceng+1,s*10+num[i][j]);
  20. exa[i]=0;
  21. }
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. while(scanf("%d",&n)!=EOF)
  28. {
  29. memset(vis,0,sizeof(vis));
  30. memset(exa,0,sizeof(exa));
  31. for(int i=1;i<=n;i++)
  32. {
  33. for(int j=1;j<=6;j++)
  34. {
  35. scanf("%d",&num[i][j]);
  36. }
  37. }
  38. dfs(0,0);
  39. int mark;
  40. for(int i=1;i<1000;i++)
  41. {
  42. if(!vis[i])
  43. {
  44. mark=i-1;
  45. break;
  46. }
  47. }
  48. printf("%d\n",mark);
  49. }
  50. return 0;
  51. }

  

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
  1. 3
    0 1 2 3 4 5
    6 7 8 9 0 1
    2 3 4 5 6 7
Output
  1. 87
Input
  1. 3
    0 1 3 5 6 8
    1 2 4 5 7 8
    2 3 4 6 7 9
Output
  1. 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不存在。
搜索里面,注意一个魔方不能同时用上面的两个数,所以要用东西标记这个魔方是否用过了

寒假集训——搜索 D - Cubes for Masha的更多相关文章

  1. 寒假集训——搜索 B - Sudoku

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream&g ...

  2. codeforces 887B Cubes for Masha 两种暴力

    B. Cubes for Masha time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  4. HZNU-ACM寒假集训Day3小结 搜索

    简单搜索 1.DFS UVA 548 树 1.可以用数组方式实现二叉树,在申请结点时仍用“动态化静态”的思想,写newnode函数 2.给定二叉树的中序遍历和后序遍历,可以构造出这棵二叉树,方法是根据 ...

  5. 2022寒假集训day2

    day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...

  6. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  7. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  8. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  9. poj3984《迷宫问题》暑假集训-搜索进阶

    K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

随机推荐

  1. Nginx/Apache之伪静态设置 - 运维小结

    一.什么是伪静态伪静态即是网站本身是动态网页如.php..asp..aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即re ...

  2. spring-session-data-redis解决session共享的问题

    分布式系统要做到用户友好,需要对用户的session进行存储,存储的方式有以下几种: 本地缓存 数据库 文件 缓存服务器 可以看一些不同方案的优缺点 1.本地机器或者本地缓存.优点:速度快  缺点:服 ...

  3. 精读JavaScript模式(六),Memoization模式与函数柯里化的应用

    假期就这么结束了!十天假就有三天在路上,真的难受!想想假期除了看了两场电影貌似也没做什么深刻印象的事情.流浪地球,特效还是很赞,不过对于感情的描写还是逃不掉拖沓和尴尬的通病,对于国产科幻还是抱有支持的 ...

  4. js-ES6学习笔记-const命令

    1.const声明一个只读的常量.一旦声明,常量的值就不能改变. 2.const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不能留到以后赋值. 3.const的作用域与l ...

  5. 百度umeditor富文本编辑器插件扩展

    富文本编辑器在WEB开发中经常用到,个人比较喜欢用百度出的ueditor这款,ueditor这款本身支持插件扩展的,但是ueditor的mini版本 umeditor 就没有那么方便了,不过找了很多资 ...

  6. windows BLE编程 net winform 连接蓝牙4.0

    winform 程序调用Windows.Devices.Bluetoot API 实现windows下BLE蓝牙设备自动连接,收发数据功能.不需要使用win10的UWP开发. 先贴图,回头来完善代码 ...

  7. Oracle入门《Oracle介绍》第一章1-2 Oracle 实例

    Oracle实例:是后台进程和内存结构的集合 一.内存结构 1.Oracle 实例启动时分配系统全局区 a.数据库信息存储于SGA,由多个数据库进程共享 1.共享池是对SQL.PL/SQL程序进行语法 ...

  8. Linux NFS存储服务部署

    什么是NFS 中文意思是网络文件系统,主要功能是通过网络(一般是局域网)让不同主机之间可以共享文件或目录 NFS属于本地文件存储服务 缺点1: 1 2 3 4 5 6 7 windows上无法使用   ...

  9. Linux 学习记录 四(Bash 和 Shell scirpt).

    一.什么是 Shell? 狭义的shell指的是指令列方面的软件,包括基本的Linux操作窗口Bash等,广义的shell则包括 图形接口的软件,因为图形接口其实也可以操作各种驱动程序来呼叫核心进行工 ...

  10. SpringCloud(二)Eureka集群与Feign

    两个Eureka 上一篇是两个服务像一个Eureka注册,如果Eureka宕掉了就不好了,现在来搭建两个Eureka,两个服务分别像其注册.两个Eureka都用本机来模拟,用两个端口号来表示. 首先修 ...