Problem F

Biggest Number

You have a maze with obstacles and non-zero digits in it:

You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once. When you finish, you can get a number by writing down the digits you encounter in the same order as you meet them. For example, you can get numbers 9784, 4832145 etc. The biggest number you can get is 791452384, shown in the picture above.

Your task is to find the biggest number you can get.

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

Output for the Sample Input

3 7

##9784#

##123##

##45###

0 0

791452384

这道题目的话,就是让你输入的是n行m列。只有数字是可以走的,你可以从任何一个位置开始,你路过的地方的数字序列为aa[],要求输出序列比较最大的。

题意是很明确的搜索,但是问题是时间。如果我每一个地方都进行一次dfs或者bfs的话,毫无疑问,肯定会超时。所以我们的思路应该放在如何去剪枝,如何去

优化搜索的时间。最后的做法就是从每一个可以出发的地方都遍历一次,在中间加上剪枝就可以过了。

 #include <cstdio>
#include <cstring>
using namespace std;
int r,c,i,j;//行 列
char a[][]; //字符串的保存
int used[][];//路径开头的标记
int ans[];//保存最大的路径
int ansl;//保存最大路径的长度
int aa[];//记录当前查找时候的路径
int zhan[][];// 就是一个栈 用来求这一个点可以往后面走多远
int used1[][];// 还是路径的记录
int neigh[][]={{,},{,},{,-},{-,}};//可以走的4个方向
int z;//表示当前查找的路径是否比已经记录的最大路径前缀大
void search(int x,int y,int l)//搜索 x,y代表当前所在的位置 l代表当前路劲的长度
{
int i,j,top,bottom,xx,yy;
if ((l>ansl)||((l==ansl)&&(z==)))//位数比以前的大或者 位数一样 但是数值大的时候 更新ans数组 和ansl的值
{
memcpy(ans,aa,sizeof(ans));
ansl=l;
z=;
}
memset(used1,,sizeof(used1));
used1[x][y]=;
top=;bottom=;
zhan[][]=x;
zhan[][]=y;
while (top<bottom)//从当前的位置 后面还能走多少步
{
for (i=;i<;i++)
{
xx=zhan[top][]+neigh[i][];
yy=zhan[top][]+neigh[i][];
if ((xx>=)&&(xx<r)&&(yy>=)&&(yy<c)&&(a[xx][yy]!='#')&&(used[xx][yy]==)&&(used1[xx][yy]==))
{
zhan[bottom][]=xx;
zhan[bottom][]=yy;
used1[xx][yy]=;
bottom++;
}
}
top++;
}
if (l+top-<ansl) return;//如果当前长度+后继结点构成最长数长度<最大数长度 结束搜索
if ((l+top-==ansl)&&(z==-)) return;//如果当前长度+后继结点构成最长数长度=最大数长度 但是比最长数小 同样结束搜索
for (i=;i<;i++)
{
xx=x+neigh[i][];
yy=y+neigh[i][];
if ((xx>=)&&(xx<r)&&(yy>=)&&(yy<c)&&(a[xx][yy]!='#')&&(used[xx][yy]==))
{
aa[l]=a[xx][yy]-'';
used[xx][yy]=;
if (z!=)
search(xx,yy,l+);//前面的数值已经比较出大小的 后面的数值不会对大小的比较产生影响
else
if (l>=ansl)
{
z=;
search(xx,yy,l+);
z=;
}
else
{
if (aa[l]>ans[l])
{
z=;
search(xx,yy,l+);
z=;
}
else if (aa[l]==ans[l])
{
z=;
search(xx,yy,l+);
z=;
}
else
{
z=-;
search(xx,yy,l+);
z=;
}
}
used[xx][yy]=;
}
}
} int main()
{
while (~scanf("%d%d",&r,&c)&&c&&r)
{
for (i=;i<r;i++)
scanf("%s",a[i]);
memset(ans,,sizeof(ans));
ans[]=-;
ansl=;
memset(aa,,sizeof(aa));
for (i=;i<r;i++)
for (j=;j<c;j++)//遍历每一个不是#号的地方 因为这一些地方都可以作为出发点
if (a[i][j]!='#')
{
used[i][j]=; // 记录 路径的开始
aa[]=a[i][j]-''; //aa是用来保存当前搜索的路径
if (a[i][j]-''>ans[])//比较大小 z=1代表大 0代表一样 -1代表小
{
z=;
search(i,j,);//搜索
}
else if (a[i][j]-''==ans[])
{
z=;
search(i,j,);
}
else
{
z=-;
search(i,j,);
}
used[i][j]=;
}
for (i=;i<ansl;i++)
printf("%d",ans[i]);
printf("\n");
}
return ;
}

湖南省第6届程序大赛第6题 Biggest Number的更多相关文章

  1. 湖南省第6届程序大赛 Repairing a Road

    Problem G Repairing a Road You live in a small town with R bidirectional roads connecting C crossing ...

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

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

  3. csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛

    这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...

  4. 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛

    "波导杯"安徽科技学院第七届程序设计大赛 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time:  2016-0 ...

  5. 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛

    "波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time:   ...

  6. 摹客 · Veer 第二届设计大赛邀你来战!

    2018年12月,摹客设计大赛一年一度一归来. 继2017年摹客全国首届原型设计大赛成功举办后,本次大赛是摹客第二届设计大赛.大赛由摹客主办,Veer独家冠名赞助,iSlide和创客贴协办,国内多家知 ...

  7. 湖南省第十三届大学生计算机程序设计竞赛 Football Training Camp 贪心

    2007: Football Training Camp[原创-转载请说明] Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 1 ...

  8. Triangle (第8届山东省赛的某题)

    triangle(第8届山东省赛的某题) 传送门 题意:喵了个呜,这题意真是峰回路转啊.懒死了,不想描述. 做法:我们拿set或线段树维护exp的最小值,每次取出exp值最小的边,删除之.并更新这条边 ...

  9. 2018年第九届蓝桥杯B组题C++汇总解析-fishers

    2018年第九届蓝桥杯B组题C++解析-fishers 题型 第一题:第几天 第二题:明码 第三题:乘积尾零 第四题:测试次数 第五题:快速排序 第六题:递增三元组 第七题:螺旋折线 第八题:日志统计 ...

随机推荐

  1. OpenCV 学习笔记(11)像素级别指针操作

    //优化两图的连接处,使得拼接自然 void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst) { int start = MIN(c ...

  2. 7-zip命令行详解

    一.简介 7z,全称7-Zip, 是一款开源软件.是目前公认的压缩比例最大的压缩解压软件. 主要特征: # 全新的LZMA算法加大了7z格式的压缩比 # 支持格式: * 压缩 / 解压缩:7z, XZ ...

  3. 大文件上传组件webupload插件

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  4. Numpy | 04 数组属性

    NumPy 数组的维数称为秩(rank),一维数组的秩为 1,二维数组的秩为 2,以此类推. 在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions).比如说,二 ...

  5. Struts2框架的搭建

    Struts2是WebWork框架的升级版本,替代了Servlet. 由于用IDEA下载jar包失败,直接创建手动导包. 1.导包: (1)Struts2的目录结构: (2)导入jar包: 2.书写A ...

  6. 05-树9 Huffman Codes (30 分)

    In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...

  7. SpringSecurity匿名用户访问权限

    在SpringSecurity中定义一个匿名访问权限,实现未登录用户可以访问默写页面 <http use-expressions="false" entry-point-re ...

  8. jmeter(五十一)_性能测试中的服务器资源监控与分析

    概述 性能测试过程中,对服务器资源的监控是必不可少的.这里的资源又分了两块,windows和linux   linux下监控资源 访问网址http://jmeter-plugins.org/downl ...

  9. 学习HSDB

    HSDB则是在SA(Serviceability Agent)基础上包装起来的一个调试器,而SA是个非常便于探索HotSpot VM内部实现的API. Stack Memory窗口的内容有三栏: 左起 ...

  10. Tocmat 统计tomcat进程内的线程数

    获取tomcat进程pid ps -ef | grep tomcat 统计该tomcat进程内的线程个数 ps -Lf  558899 | wc -l