湖南省第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 题型 第一题:第几天 第二题:明码 第三题:乘积尾零 第四题:测试次数 第五题:快速排序 第六题:递增三元组 第七题:螺旋折线 第八题:日志统计 ...
随机推荐
- Caching POST-post是否能缓存
https://www.mnot.net/blog/2012/09/24/caching_POST One of the changes in Apple’s release of iOS6 last ...
- Boring counting HDU - 3518 (后缀自动机)
Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...
- 2019牛客国庆集训派对day3 买一送一
题目链接: 题意:有n个点,n-1条单向边,每个点都销售一类商品 问从点1开始走,买第一样商品类型为x,买第二样商品类型为y,问不同有序对<x,y>的数量 解法: col[i]表示这个点的 ...
- MongoDB shell 2 副本集方法
rs.initiate() rs.addArb() rs.help() rs.printReplicationInfo() 查看到副本集操作日志 rs.remove() 减少副本集节点 r ...
- Selenium元素定位的几种方式
一.通过id查找 例:<input id="kw" name="wd" class="s_ipt" value="" ...
- sqlg rdbms 上实现的Apache TinkerPop
sqlg 可以让关系型数据库支持Apache TinkerPop,当前支持的数据库有postgresql,hsqldb,h2,mariadb,mysql,mssqlserver 以下是一个简单的使用 ...
- MySQL中自增ID起始值修改方法
在实际测试工作过程中,有时因为生产环境已有历史数据原因,需要测试环境数据id从某个值开始递增,此时,我们需要修改数据库中自增ID起始值,下面以MySQL为例: 表名:users; 建表时添加: ); ...
- js对象属性名以数字开头如何获取、js属性名以数字开头、missing ) after argument list
js对象中属性名以数字开头引发的报错 :missing ) after argument list var ChineseDistricts = { : { : '北京市', : '天津市', : ' ...
- vue中使用时间插件、vue使用laydate
<input id="time1" readonly="readonly" placeholder="这里选择时间" v-model= ...
- web3 编译部署调用合约
//导入solc 编译器 let solc = require('solc') let fs = require('fs') //读取合约 let sourceCode = fs.readFileSy ...