湖南省第6届程序大赛第6题 Biggest Number
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的更多相关文章
- 湖南省第6届程序大赛 Repairing a Road
Problem G Repairing a Road You live in a small town with R bidirectional roads connecting C crossing ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛
这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...
- 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
"波导杯"安徽科技学院第七届程序设计大赛 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time: 2016-0 ...
- 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
"波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time: ...
- 摹客 · Veer 第二届设计大赛邀你来战!
2018年12月,摹客设计大赛一年一度一归来. 继2017年摹客全国首届原型设计大赛成功举办后,本次大赛是摹客第二届设计大赛.大赛由摹客主办,Veer独家冠名赞助,iSlide和创客贴协办,国内多家知 ...
- 湖南省第十三届大学生计算机程序设计竞赛 Football Training Camp 贪心
2007: Football Training Camp[原创-转载请说明] Submit Page Summary Time Limit: 1 Sec Memory Limit: 1 ...
- Triangle (第8届山东省赛的某题)
triangle(第8届山东省赛的某题) 传送门 题意:喵了个呜,这题意真是峰回路转啊.懒死了,不想描述. 做法:我们拿set或线段树维护exp的最小值,每次取出exp值最小的边,删除之.并更新这条边 ...
- 2018年第九届蓝桥杯B组题C++汇总解析-fishers
2018年第九届蓝桥杯B组题C++解析-fishers 题型 第一题:第几天 第二题:明码 第三题:乘积尾零 第四题:测试次数 第五题:快速排序 第六题:递增三元组 第七题:螺旋折线 第八题:日志统计 ...
随机推荐
- ajax异步
异步与同步 这就是生活中的同步 在JavaScript语言中,同步和异步的概念刚好相反. 这JavaScript中同步就是:你不执行完上面的代码,那么下面的代码你就别执行:一步一步 ...
- MongoDB shell 0 集合方法
方法名 描述 db.collection.aggregate() 聚合,主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果 db.collection.bulkWrite() 批量写入 ...
- 验证码破解 | Selenium模拟登陆12306
12306官网登录的验证码破解比较简单,验证码是常规的点触类型验证码,使用超级鹰识别率比较高. 思路: (1)webdriver打开浏览器: (2)先对整个屏幕截屏,通过标签定位找到验证码图片,并定位 ...
- 20101010 exam
目录 2018 10.10 exam 解题报告 T1:LOJ #10078 新年好 题目描述(原题来自:CQOI 2005): 输入格式: 输出格式: 样例输入: 样例输出: 数据范围与提示: 思路: ...
- C函数之index、strtoul
index函数 函数定义: #include<strings.h> char *index(const char *s, int c); 函数说明: 找出参数s字符串中第一个出现参数c的地 ...
- SpringCloud:gateway网关模块启动报错
1.错误信息 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with na ...
- [Gamma阶段]第六次Scrum Meeting
Scrum Meeting博客目录 [Gamma阶段]第六次Scrum Meeting 基本信息 名称 时间 地点 时长 第六次Scrum Meeting 19/06/1 大运村寝室6楼 25min ...
- 阿里云ECS服务器相关配置以及操作---上(初学者)
最近买了一台阿里云的ECS服务器 linux系统 centos镜像,把我相关的一些操作记录下来,供大家参考,不足之处欢迎指正. 首先买的过程就不用介绍了,根据自己的实际需要选择自己想要的配置,点击付钱 ...
- mstar安卓智能电视方案源代码常用修改
优先 替换 Supernova\projects\customerinfo\inc\Customer_Info.h替换 内核中linux/drivers/mmc/core/mmc.c文件 1, key ...
- 浅析String.intern()方法
1.String类型“==”比较样例代码如下:package com.luna.test;public class StringTest { public static void main(Strin ...