Problem UVA11882-Biggest Number

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

3 7
##9784#
##123##
##45###
0 0
 

 Sample Output

791452384

题解:一看题目觉得是个大水题,dfs一下就行,然后果断TLE,这个题对效率要求还是比较高的,两个比较重要的剪枝:

1、如果剩余最长可走长度加上目前已有长度小于答案的长度,直接return.

2、如果剩余最长可走长度加上目前已有长度等于答案的长度,比较字典序,如果答案更优就retrun.

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = ;
  6.  
  7. int n, m, cnt;
  8. int head, tail;
  9. pair<int,int> que[maxn*maxn];
  10. int dx[] = { ,-,, };
  11. int dy[] = { ,,-, };
  12. char gra[maxn][maxn];
  13. bool vis[maxn][maxn],vvis[maxn][maxn];
  14. string ans, tmp;
  15.  
  16. int h(int x,int y) {
  17. head = tail = ;
  18. int cnt = ;
  19. que[tail++] = make_pair(x, y);
  20. memcpy(vvis, vis, sizeof(vis));
  21.  
  22. while (head < tail) {
  23. pair<int, int> temp = que[head++];
  24. for (int i = ; i < ; i++) {
  25. int xx = temp.first + dx[i], yy = temp.second + dy[i];
  26. if ( <= xx && <= yy && xx < n && yy < m && !vvis[xx][yy] && gra[xx][yy] != '#') {
  27. vvis[xx][yy] = true;
  28. cnt++;
  29. que[tail++] = make_pair(xx, yy);
  30. }
  31. }
  32. }
  33. return cnt;
  34. }
  35.  
  36. void update(const string& s) {
  37. int len1 = s.size(), len2 = ans.size();
  38. if (len2 < len1 || (len2 == len1 && ans < s)) ans = s;
  39. }
  40.  
  41. void dfs(int x,int y,string s,int deep) {
  42. int hh = h(x, y);
  43. int l = ans.size();
  44. if (deep + hh < l) return;
  45. if (deep + hh == l) {
  46. if (s + "z" < ans) return;
  47. }
  48.  
  49. update(s);
  50.  
  51. for (int i = ; i < ; i++) {
  52. int xx = x + dx[i], yy = y + dy[i];
  53. if ( <= xx && <= yy && xx < n && yy < m && !vis[xx][yy] && gra[xx][yy] != '#') {
  54. vis[xx][yy] = true;
  55. dfs(xx, yy, s + gra[xx][yy], deep + );
  56. vis[xx][yy] = false;
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. //freopen("input.txt", "r", stdin);
  64. while (~scanf("%d%d", &n, &m) && (n || m)) {
  65. ans = "";
  66. for (int i = ; i < n; i++) {
  67. scanf("%s", gra[i]);
  68. }
  69.  
  70. for (int i = ; i < n; i++) {
  71. for (int j = ; j < m; j++) {
  72. if (isdigit(gra[i][j])) {
  73. tmp = "";
  74. tmp += gra[i][j];
  75. vis[i][j] = true;
  76. dfs(i, j, tmp, );
  77. vis[i][j] = false;
  78. }
  79. }
  80. }
  81. cout << ans << endl;
  82. }
  83. return ;
  84. }

UVA11882-Biggest Number(DFS+最优化剪枝)的更多相关文章

  1. UVA-11882 Biggest Number (DFS+剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  2. UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  3. 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

  4. 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 ...

  5. UVa11882,Biggest Number

    搜索+剪枝 如此水的一个题,居然搞了一上午 出错在bfs与dfs时共用了一个vis数组,导致bfs完后返回dfs应该能访问到的点访问不到 自己想怎么剪枝,想了几个剪枝方法,又证明,又推翻,再想,再证明 ...

  6. 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)

    Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...

  7. 湖南省第6届程序大赛第6题 Biggest Number

    Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...

  8. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...

  9. UVA10624 - Super Number(dfs)

    题目:UVA10624 - Super Number(dfs) 题目大意:给你n和m要求找出这种m位数,从第n位到第m位都满足前i位是能够被i整除,假设没有这种数,输出-1.有多个就输出字典序最小的那 ...

随机推荐

  1. Github被微软收购,这里整理了16个替代品

    微软斥资75亿美元收购以后,鉴于微软和开源竞争的历史,很多开发者都感到惊恐.毕竟,互联网上最大的一块可以自由的净土被微软染指,宝宝不开森.如果你真的担心微软会对Github有所动作,那么这里我列举了1 ...

  2. 使用mybatis开发dao问题总结

    代码片段: @Override public User getUserById(Integer id) { SqlSession sqlSession = sqlSessionFactory.open ...

  3. HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列

    在HTML中表格是作为一个整体来解析的,解析完才会在页面显示,如果表格很复杂很长,加载时间很长,用户体验就不好.所以这里就要用到表格结构标签,解析一部分就显示一部分,不用等表格全部加载完再显示. 表格 ...

  4. 亲测:LNMP环境下,解决项目缓冲慢、502以及配置https的问题

    在做的项目在nginx下访问缓冲时间过长,明显比apache下访问蛮11倍有余, 解决办法: 1增加nginx的upstream,其中upstream中为php-cgi的地址: 2利用nginx作为反 ...

  5. BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)

    题意 题目链接 Sol 把式子拆开,就是求这个东西 \[\sum_{i = 0} ^n \sum_{j = 1}^{a + id} \sum_{x =1}^j x^k \pmod P\] 那么设\(f ...

  6. C#基础(204)--对象初始化器,基本数据类型与引用数据类型特点总结,ref,out关键字的使用

    对象初始化器: 对象在创建过程中也可以使用对象初始化器完成“属性的初始化” Student stu =new Student(){ StudentId=, StudentName="张三&q ...

  7. 行业观察报告:从SAAS困局看行业趋势 ZT

    企业管理软件的演化过程 第一阶段:独立开发 典型代表:IBM 这个阶段是将企业的信息化需求整合成硬件+软件的一体化解决方案,从零开始设计开发,适用于通讯.电力.交通等基础设施建设项目.这个阶段的特点是 ...

  8. Visual Studio 20周年,我和VS不得不说的故事

    Visual Studio 2017正式版已如期发布(点击这里查看发布全记录)!自去年 11 月正式宣布 Visual Studio 项目之后,微软终于正式推出了“宇宙最强集成开发环境(IDE)”的最 ...

  9. 切换横竖屏的时候Activity的生命周期变化情况

    关于这个,有个博客说得比较清楚:http://blog.csdn.net/wulianghuan/article/details/8603982,直接给出链接,哈哈哈.

  10. 2014/08/31 Zushi

    今天是逗子森户海滨浴场开放的最后一天,趁着最后的光景来这里透透气. 在学皮划艇准备下海的人们,貌似还挺有趣. 来自云端的上帝之手. 谁愿意和我一起向着夕阳弄桨. 夕阳西下,那里是家乡的方向. 灯塔和神 ...