UVA11882-Biggest Number(DFS+最优化剪枝)
Accept: 177 Submit: 3117
Time Limit: 1000 mSec Memory Limit : 128MB
Problem Description
Input
There will be at most 25 test cases. Each test begins with two integers R and C (2 ≤ R,C ≤ 15, R∗C ≤ 30), the number of rows and columns of the maze. The next R rows represent the maze. Each line contains exactly C characters (without leading or trailing spaces), each of them will be either ‘#’ or one of the nine non-zero digits. There will be at least one non-obstacle squares (i.e. squares with a non-zero digit in it) in the maze. The input is terminated by a test case with R = C = 0, you should not process it.
Output
For each test case, print the biggest number you can find, on a single line.
Sample Input
Sample Output
791452384
题解:一看题目觉得是个大水题,dfs一下就行,然后果断TLE,这个题对效率要求还是比较高的,两个比较重要的剪枝:
1、如果剩余最长可走长度加上目前已有长度小于答案的长度,直接return.
2、如果剩余最长可走长度加上目前已有长度等于答案的长度,比较字典序,如果答案更优就retrun.
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = ;
- int n, m, cnt;
- int head, tail;
- pair<int,int> que[maxn*maxn];
- int dx[] = { ,-,, };
- int dy[] = { ,,-, };
- char gra[maxn][maxn];
- bool vis[maxn][maxn],vvis[maxn][maxn];
- string ans, tmp;
- int h(int x,int y) {
- head = tail = ;
- int cnt = ;
- que[tail++] = make_pair(x, y);
- memcpy(vvis, vis, sizeof(vis));
- while (head < tail) {
- pair<int, int> temp = que[head++];
- for (int i = ; i < ; i++) {
- int xx = temp.first + dx[i], yy = temp.second + dy[i];
- if ( <= xx && <= yy && xx < n && yy < m && !vvis[xx][yy] && gra[xx][yy] != '#') {
- vvis[xx][yy] = true;
- cnt++;
- que[tail++] = make_pair(xx, yy);
- }
- }
- }
- return cnt;
- }
- void update(const string& s) {
- int len1 = s.size(), len2 = ans.size();
- if (len2 < len1 || (len2 == len1 && ans < s)) ans = s;
- }
- void dfs(int x,int y,string s,int deep) {
- int hh = h(x, y);
- int l = ans.size();
- if (deep + hh < l) return;
- if (deep + hh == l) {
- if (s + "z" < ans) return;
- }
- update(s);
- for (int i = ; i < ; i++) {
- int xx = x + dx[i], yy = y + dy[i];
- if ( <= xx && <= yy && xx < n && yy < m && !vis[xx][yy] && gra[xx][yy] != '#') {
- vis[xx][yy] = true;
- dfs(xx, yy, s + gra[xx][yy], deep + );
- vis[xx][yy] = false;
- }
- }
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- while (~scanf("%d%d", &n, &m) && (n || m)) {
- ans = "";
- for (int i = ; i < n; i++) {
- scanf("%s", gra[i]);
- }
- for (int i = ; i < n; i++) {
- for (int j = ; j < m; j++) {
- if (isdigit(gra[i][j])) {
- tmp = "";
- tmp += gra[i][j];
- vis[i][j] = true;
- dfs(i, j, tmp, );
- vis[i][j] = false;
- }
- }
- }
- cout << ans << endl;
- }
- return ;
- }
UVA11882-Biggest Number(DFS+最优化剪枝)的更多相关文章
- UVA-11882 Biggest Number (DFS+剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- UVA 11882 Biggest Number(搜索+剪枝)
You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the ...
- UVa11882,Biggest Number
搜索+剪枝 如此水的一个题,居然搞了一上午 出错在bfs与dfs时共用了一个vis数组,导致bfs完后返回dfs应该能访问到的点访问不到 自己想怎么剪枝,想了几个剪枝方法,又证明,又推翻,再想,再证明 ...
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...
- 湖南省第6届程序大赛第6题 Biggest Number
Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...
- 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)
主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...
- UVA10624 - Super Number(dfs)
题目:UVA10624 - Super Number(dfs) 题目大意:给你n和m要求找出这种m位数,从第n位到第m位都满足前i位是能够被i整除,假设没有这种数,输出-1.有多个就输出字典序最小的那 ...
随机推荐
- Github被微软收购,这里整理了16个替代品
微软斥资75亿美元收购以后,鉴于微软和开源竞争的历史,很多开发者都感到惊恐.毕竟,互联网上最大的一块可以自由的净土被微软染指,宝宝不开森.如果你真的担心微软会对Github有所动作,那么这里我列举了1 ...
- 使用mybatis开发dao问题总结
代码片段: @Override public User getUserById(Integer id) { SqlSession sqlSession = sqlSessionFactory.open ...
- HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列
在HTML中表格是作为一个整体来解析的,解析完才会在页面显示,如果表格很复杂很长,加载时间很长,用户体验就不好.所以这里就要用到表格结构标签,解析一部分就显示一部分,不用等表格全部加载完再显示. 表格 ...
- 亲测:LNMP环境下,解决项目缓冲慢、502以及配置https的问题
在做的项目在nginx下访问缓冲时间过长,明显比apache下访问蛮11倍有余, 解决办法: 1增加nginx的upstream,其中upstream中为php-cgi的地址: 2利用nginx作为反 ...
- BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)
题意 题目链接 Sol 把式子拆开,就是求这个东西 \[\sum_{i = 0} ^n \sum_{j = 1}^{a + id} \sum_{x =1}^j x^k \pmod P\] 那么设\(f ...
- C#基础(204)--对象初始化器,基本数据类型与引用数据类型特点总结,ref,out关键字的使用
对象初始化器: 对象在创建过程中也可以使用对象初始化器完成“属性的初始化” Student stu =new Student(){ StudentId=, StudentName="张三&q ...
- 行业观察报告:从SAAS困局看行业趋势 ZT
企业管理软件的演化过程 第一阶段:独立开发 典型代表:IBM 这个阶段是将企业的信息化需求整合成硬件+软件的一体化解决方案,从零开始设计开发,适用于通讯.电力.交通等基础设施建设项目.这个阶段的特点是 ...
- Visual Studio 20周年,我和VS不得不说的故事
Visual Studio 2017正式版已如期发布(点击这里查看发布全记录)!自去年 11 月正式宣布 Visual Studio 项目之后,微软终于正式推出了“宇宙最强集成开发环境(IDE)”的最 ...
- 切换横竖屏的时候Activity的生命周期变化情况
关于这个,有个博客说得比较清楚:http://blog.csdn.net/wulianghuan/article/details/8603982,直接给出链接,哈哈哈.
- 2014/08/31 Zushi
今天是逗子森户海滨浴场开放的最后一天,趁着最后的光景来这里透透气. 在学皮划艇准备下海的人们,貌似还挺有趣. 来自云端的上帝之手. 谁愿意和我一起向着夕阳弄桨. 夕阳西下,那里是家乡的方向. 灯塔和神 ...