2019河北省大学生程序设计竞赛(重现赛) L题-smart robot(深度优先搜索)

题目链接:https://ac.nowcoder.com/acm/contest/903/L

题意:给你 n * n的方阵,你可以从任意一个数字开始走,可以走上下左右四个方向,走过的数字会被拼合,拼合的数字没有前导0,问最小不能拼合出来的数字是多少?

思路:开始就认为是一道深搜题,后面自己想太多就没去试,醉了orz! 这题就是把每个数字作为起点开始搜索,把已经搜索过的数字进行标记,然后从0开始检索,输出第一个未出现的数字就是答案。搜索的话我代码是搜5位数的所有组合,事实上这道题搜索3位数的所有组合就能过,搜索6位数就会TLE。下图是我的测试图:

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<set>
 4 #include<cstring>
 5 using namespace std;
 6 int n;
 7 int vis[60][60];
 8 int dx[4] = {0,1,0,-1};
 9 int dy[4] = {-1,0,1,0};
10 set< int > st;
11 bool check(int x,int y)
12 {
13     if(x >= 0 && x < n && y >= 0 && y < n) return true;
14     else return false;
15 }
16 void dfs(int x,int y,int ans,int dep)
17 {
18     st.insert(ans);
19     for(int i = 0;i < 4;i++)
20     {
21         int nx = x + dx[i],ny = y + dy[i];
22         if(check(nx,ny) && dep > 0)
23             dfs(nx,ny,ans*10 + vis[nx][ny],dep-1);
24     }
25 }
26 int main()
27 {
28     scanf("%d",&n);
29     for(int i = 0;i < n;i++)
30     {
31         for(int j = 0 ;j < n;j ++)
32         {
33             scanf("%d",&vis[i][j]);
34         }
35     }
36     for(int i = 0;i < n;i++)
37     {
38         for(int j = 0 ;j < n;j ++)
39         {
40             dfs(i,j,vis[i][j],5);//这里搜索5位数的组合
41         }
42     }
43     int cnt = 0;
44     while(st.count(cnt) != 0) cnt++;
45     cout << cnt << endl;
46     st.clear();
47     return 0;
48 }

原文地址:https://www.cnblogs.com/Carered/p/10926136.html

时间: 2024-08-29 23:52:06

2019河北省大学生程序设计竞赛(重现赛) L题-smart robot(深度优先搜索)的相关文章

第八届福建省大学生程序设计竞赛-重现赛

第八届福建省大学生程序设计竞赛-重现赛 B   计算几何 题意:问两个三角形是相交.包含还是相离. tags:套板子..求出相交的面积,再判断一下 /* 多边形相交面积模板 */ #define maxn 510 const double eps=1E-8; int sig(double d){ return(d>eps)-(d<-eps); } struct Point{ double x,y; Point(){} Point(double x,double y):x(x),y(y){} b

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5929 Problem Description Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ? PUSH x: put x on the top of the stack, x must be 0 or 1.? POP

ZZUOJ - 1245 - 寻找幸福的小L ( 郑州大学第八届ACM大学生程序设计竞赛正式赛F题)

1245: 寻找幸福的小L Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 60  Solved: 14 [Submit][Status][Web Board] Description 小L最近看上了一个女同学叫小A,但是小A是个高冷的姑娘,所以她给小L出了个难题,她给小L留下了一张纸条,上面只有一个坐标,和一个时间,所以小L需要在规定时间找到这个坐标的地点,并在那个时间到哪个地点等待小A,但是很无奈,小L不是一个地理通,所以他找到了小A的室友,想

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1005

链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 #include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008

链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从‘栈’顶到低的运算顺序结果是多少 解法:根据位运算,发现出现0,结果就是1,那么就记录两端0的位置就好,中间不管出现什么,结果大部分都是1,考虑还有反转操作,使用双端队列,用flag标记反转后的情况,然后根据需要添加元素记录位置,最后根据标记,出现元素等进行讨论计算 #include <iostream> #include <dequ

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1003

链接http://acm.hdu.edu.cn/showproblem.php?pid=5924 题意:根据公式求C,D 解法:打表找规律 #include <bits/stdc++.h> using namespace std; #define ll long long int main() { int t,cnt=1; scanf("%d",&t); while(t--) { ll a,b; scanf("%I64d%I64d",&a

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1001

链接http://acm.hdu.edu.cn/showproblem.php?pid=5922 题意:最小生成树,但边的权值是连接两点的最小公倍数 解法:不要真的写最小生成树啦,只要其他点和第一点相连,边的权值就是最小的,相加就好了 #include <bits/stdc++.h> using namespace std; #define ll long long int main() { int t,cnt=1; scanf("%d",&t); while(t-

RunningMan【第六届福建省大学生程序设计竞赛-重现赛】

RunningMan Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 2221 64-bit integer IO format: %I64d      Java class name: Main Prev Submit Status Statistics Discuss Next ZB loves watching RunningMan! There's a ga

ZZUOJ-1194-ARM立即数寻址 (郑州大学第七届ACM大学生程序设计竞赛正式赛F题)

Problem F: ARM立即数寻址 Time Limit: 4 Sec  Memory Limit: 128 MB Submit: 53  Solved: 12 [Submit][Status][Web Board] Description 在ARM处理器立即数寻址方式中,立即数是由一个8位的无符号常数(大于等于0,小于等于0xff),先扩展为32位,然后循环右移偶数位得到.所以类似0x101,0x102,0xFF1,0xFF04,0x8000007F等都是无效的立即数,而像0xFF,0x3