题意

给出如图案例,要你从某一点开始走,一直走到极限(即无法再进行扩展),这时你走过的点会连成一个数,不同的走法当然会有不同的数,要求是输出最大的数(注意每个方块走过一次就不能再走)

思路

•1.枚举所有的点作为起点,然后求从这个点所能得到的最大数
•2.然后是使用DFS求从某一点可以到达的最大数
可是仅仅使用DFS是会超时的,

所以,需要优化剪枝

Dfs的过程就是构建和遍历解答树的过程,在进行深度优先搜索时有一些分叉是可以判断出无需遍历的,这时就可以把这一部分跳过,continue掉

剪枝: 首先一个数它与另一个数最大的区别在于长度,即使它是最大的二位数,我是最小的三位数,我依然大于它,因此可以使用BFS,使用它去判断从这一点所能到达的最大长度,如果这个长度小于我之前所保留的最大长度,那么就不用再搜了,直接跳过。

还有一种情况,就是BFS出来的数长度和之前所保留的最大数长度相等呢?既然已经使用BFS做搜索预判,那么就不如在记录好从那一点所到达的最长距离所形成的数记录下来。然后比较这个数和保留的最大数,如果它小,那就甭走这走一步了

#include"iostream"
#include"cstring"
#include"queue"
#include"ctype.h"
#include"algorithm"
using namespace std; char a[][];
int can[];
int book[][];
int book2[][];
int m,n;
int nex[][]= {{,},{,},{-,},{,-}};
int tx,ty; bool cmp(int a,int b)
{
return a>b;
} typedef struct Node
{
int no[], len;
void Init()
{
len = ;
}
bool operator < (const Node &rhs) const
{
if(len != rhs.len) return len < rhs.len;
for(int i = ; i < len; ++i)
if(no[i] != rhs.no[i]) return no[i] < rhs.no[i];
return false;
}
} Node; Node ans,now; int bfs(int x,int y)
{
queue<int> que;
que.push(x * + y);
int f = ;
can[] = a[x][y] - '';
memset(book2, , sizeof(book2));
book2[x][y] = ;
while(!que.empty())
{
int tmp = que.front();
que.pop();
int nx = tmp /, ny = tmp % ;
for(int i = ; i < ; ++i)
{
int px = nx + nex[i][], py = ny + nex[i][];
if(!isdigit(a[px][py]) || book[px][py] || book2[px][py]) continue;
book2[px][py] = ;
can[f++] = a[px][py] - '';
que.push(px * + py);
}
}
return f;
} void dfs(int x,int y)
{
now.no[now.len++] = a[x][y] - '';
book[x][y] = ;
for(int i = ; i < ; ++i)
{
int px = x +nex[i][], py = y + nex[i][];
if(!isdigit(a[px][py]) || book[px][py]) continue;
int wantlen = bfs(px, py);
if(now.len + wantlen < ans.len) continue;
if(now.len + wantlen == ans.len)
{
sort(can, can + wantlen);
Node tmp = now;
for(int i = wantlen - ; i >= ; --i) tmp.no[tmp.len++] = can[i];
if(tmp < ans) continue;
}
dfs(px, py);
}
if(ans < now) ans = now;
--now.len;
book[x][y] = false;
} int main()
{
while(cin>>m>>n&&m)
{
memset(a,,sizeof(a));
memset(book,,sizeof(book));
for(int i=; i<m; i++) scanf("%s",a[i]);
ans.Init();
now.Init();
for(int j=; j<m; j++)
for(int k=; k<n; k++)
if(isdigit(a[j][k])) dfs(j,k); for(int kk=; kk<ans.len; kk++) cout<<ans.no[kk];
cout<<endl;
}
return ;
}

第七章习题G题的更多相关文章

  1. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  2. C和指针第七章第五题

    实现一个简化的printf函数,能够处理%d,%f,%s,%c等格式. /*************************************************************** ...

  3. Learning Perl 第六章习题第一题

    按照first name找last name 知识点: 1. hash的使用和初始化 2. 使用exists函数检测hash中的键是否存在

  4. Learning Perl 第九章习题第二题

    把输入文件中的所有Fred换成Larry, 不区分大小写. 知识点 1. 文本文件读写 2. 简单的正则替换 3. unless 的用法 4. $_ 的用法

  5. Perl语言入门:第七章习题:输出文件中包含一个大写字母的所有行,不输出一行的内容全是大写的

    文件内容: bash-2.03$ cat file_4_ex_ch7.txt anonymous attribute demolition grammar rules indices refernce ...

  6. java编程思想第四版第七章习题

    (略) (略) (略) (略) 创建两个带有默认构造器(空参数列表)的类A和类B.从A中继承产生一个名为C的新,并在C内创建一个B类的成员.不要给C编写构造器.创建一个C类的对象并观察其结果. pac ...

  7. PythonCrashCourse 第七章习题

    编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如"Let me see if I can find you a Subaru" car =input("Wha ...

  8. 视觉slam十四讲第七章课后习题6

    版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8545450.html 6.在PnP优化中,将第一个相机的观测也考虑进来,程序应如何 ...

  9. 视觉slam十四讲第七章课后习题7

    版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/newneul/p/8544369.html  7.题目要求:在ICP程序中,将空间点也作为优化变量考虑进来 ...

随机推荐

  1. 【react-native】持续踩坑总结

    陆陆续续的已经接触了RN快3个月,整体的感受...感觉在调试兼容andorid问题的时候就像回到了IE时代. 本来想按自己踩坑的路径持续更新一些记录,但是,现实是坑太多,还是统一写一篇汇总一下吧(鉴于 ...

  2. ural1076 Trash 垃圾

    Description You were just hired as CEO of the local junkyard.One of your jobs is dealing with the in ...

  3. CF919D Substring

    思路: 拓扑排序过程中dp.若图有环,返回-1. 实现: #include <bits/stdc++.h> using namespace std; ; vector<int> ...

  4. [BZOJ2190][SDOI2008]仪仗队 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2190 看到这道题首先想到了NOI2010的能量采集,这不就是赤裸裸的弱化版吗?直接上莫比乌 ...

  5. iOS programming UITabBarController

    iOS programming UITabBarController 1.1 View controllers become more interesting when the user's acti ...

  6. ubuntu个人初始配置记录

    1.安装vim编辑器 sudo apt-get install vim vim-gnome. vim有vim(vim-basic),vim-tiny,vim-gnome(gvim)等多个版本,安装ub ...

  7. [转]c++应用程序文件的编译过程

    原文地址 这里讲下C++文件的编译过程及其中模板的编译过程: 一:一般的C++应用程序的编译过程.    一般说来,C++应用程序的编译过程分为三个阶段.模板也是一样的. 在cpp文件中展开inclu ...

  8. React和webpack解决 waiting for roots to load...to reload the inspector

    使用chrome调试工具,react-devtools总是显示 "waiting for roots to load...to reload the inspector" and ...

  9. Java 字符串格式化 String.format() 的使用

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得c语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  10. 【转】c语言中的定义和声明

    1. 变量的定义.声明 变量的声明有两种情况: 一种是需要建立存储空间的.例如:int  a.在声明的时候就已经建立了存储空间.这种声明是"定义性声明(defining declaratio ...