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. mui 等待动画loading mui.showLoading

    显示加载框:mui.showLoading("正在加载..","div"); //加载文字和类型,plus环境中类型为div时强制以div方式显示隐藏加载框:m ...

  2. 树上背包DP Luogu P2014 选课

    #include <cstdio> #include <cctype> #include <cstring> #include <algorithm> ...

  3. Substrings SPOJ - NSUBSTR (后缀自动机)

    Substrings \[ Time Limit: 100ms\quad Memory Limit: 1572864 kB \] 题意 给出一个长度为 \(250000\) 的字符串,求出所有 \(x ...

  4. Docker for Window 数据库路径注意事项

    Windows和Linux的文件路径格式不同,以第一行为例的话要改为//D/work/fronent-api:/www //D:标识D盘,如果是其他盘,如F盘://F/

  5. xsxsxs

    def getdecrype(l): a= length="" ): if(l[a]=="x"): break else: length+=l[a] a+= h ...

  6. Shell 变量自增实现方法

    i=`expr $i + `; let i+=; ((i++)); i=$[$i+]; i=$(( $i + )) 参考: https://www.cnblogs.com/faithfu/p/9472 ...

  7. Excel 截取字符,判断县区 城市。

    https://jingyan.baidu.com/article/624e7459aa90e434e8ba5a8a.html https://jingyan.baidu.com/article/9f ...

  8. js使用WebSocket,java使用WebSocket

    js使用WebSocket,java使用WebSocket 创建java服务端代码 import java.net.InetSocketAddress; import org.java_websock ...

  9. git rebase 多分支操作

    - git rebase and git merge 区别 这一次彻底搞懂 Git Rebase - git在工作中正确的使用方式----git rebase篇 Git 操作假设Git目前只有一个分支 ...

  10. 【转】Android Fastboot 与 Recovery 和刷机

    1. 首先来看下Android系统的分区:   Android系统的分区.jpg   Android分区解释.png 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hb ...