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. Mysql复制一条或多条记录并插入表|mysql从某表复制一条记录到另一张表

    Mysql复制一条或多条记录并插入表|mysql从某表复制一条记录到另一张表 一.复制表里面的一条记录并插入表里面    ① insert into article(title,keywords,de ...

  2. 客户端本地存储,web存储,localStorage

    HTML5 LocalStorage 本地存储 说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就 ...

  3. mybatis include refid="Base_Column_List"含义

    <sql id="Base_Column_List" > collegeID, collegeName </sql> <select id=" ...

  4. [Algorithm] 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. be of + 名词

    语法: “be of +抽象名词(词组)” 表示主语的某种形状或特征,相当于 "be+形容词" 例如: be of value=be valuable  : be of inter ...

  6. [RN] React Native 封装选择弹出框(ios&android)

    之前看到react-native-image-picker中自带了一个选择器,可以选择拍照还是图库,但我们的项目中有多处用到这个选择弹出框,所以就自己写了一下,最最重要的是ios和Android通用. ...

  7. 封装好的observer.js,用于非父子组件传值,直接调用$on和$emit方法

    const eventList = {} const $on = (eventName,callback)=>{ if(!eventList[eventName]){ eventList[eve ...

  8. Sublime Text 3安装Package Control并安装Processing插件

    由于PDE编辑界面对中文的支撑太差,于是想换到ST3来编辑代码,结果导致了噩梦的开始. 首先,找不到“Package Control”!!! 这还怎么玩~ 于是打开http://packagecont ...

  9. selenuim自动化爬取汽车在线谷米爱车网车辆GPS数据爬虫

    #为了实时获取车辆信息,以及为了后面进行行使轨迹绘图,写了一个基于selelnium的爬虫爬取了车辆gps数据. #在这里发现selenium可以很好的实现网页解析和处理js处理 #导包 import ...

  10. shell 换行与不换行

    test.sh: echo -e "hello w\norld!"echo -e "hello w\c"echo "orld!" 输出 bo ...