Hidden Number
2015-08-27 17:48
183 查看
题目描述
Yourjobistofindoutthesecretnumberhiddeninamatrix,eachofwhoseelementisadigit('0'-'9')oraletter('A'-'Z').YoucanseeanexamplematrixinFigure1.Thehiddennumberandothernon-secretonesarecodedinamatrixassequencesofdigitsinadecimalformat.YoushouldonlyconsidersequencesofdigitsD1D2...DnsuchthatDk+1(1<=k<n)iseitherrightnexttoorimmediatelybelowDkinthematrix.
Thesecretyouareseekingisthelargestnumbercodedinthismanner.
FourcodednumbersinthematrixinFigure1,i.e.,908820,23140037,23900037,and9930,aredepictedinFigure2.Asyoumaysee,ingeneral,twoormorecodednumbersmayshareacommonsubsequence.Inthiscase,thesecretnumberis23900037,which
isthelargestamongthesetofallcodednumbersinthematrix.
Incontrast,thesequencesillustratedinFigure3shouldbeexcluded:908A2includesaletter;thefifthdigitof23149930isabovethefourth;thethirddigitof90037isbelowrightofthesecond.
Writeaprogramtofigureoutthesecretnumberfromagivenmatrix.
输入
Theinputconsistsofmultipledatasets,eachdatasetrepresentingamatrix.Theformatofeachdatasetisasfollows.
WH
C11C12...C1W
C21C22...C2W
...
CH1CH2...CHW
Inthefirstlineofadataset,twopositiveintegersWandHaregiven.Windicatesthewidth(thenumber
ofcolumns)ofthematrix,andHindicatestheheight(thenumberofrows)ofthematrix.W+Hisless
thanorequalto70.
Hlinesfollowthefirstline,eachofwhichcorrespondstoarowofthematrixintoptobottomorder.The
i-throwconsistsofWcharactersCi1Ci2...CiWinlefttorightorder.Youmayassumethatthematrix
includesatleastonenon-zerodigit.
Followingthelastdataset,twozerosinalineindicatetheendoftheinput.
输出
Foreachdataset,printthehiddennumberonaline.Leadingzerosshouldbesuppressed.样例输入
749R2A9930E314A08A900DE820R03767JH03HEID77220DA1AH30C9G599971ACA7EAIAHLBEM202A1234567891234CBDEGHBDEDF90803426509149900
样例输出
2390003777197112345908034265091499
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stdlib.h>
#include<iostream>
usingnamespacestd;
charans[77];
intanslen;
charmap[77][77];
structs
{
intx,y,len;
charstr[77];
}a,temp;
intn,m;
voidbfs(intx,inty)
{
// memset(vis,0,sizeof(vis));
a.x=x;
a.y=y;
memset(a.str,0,sizeof(a.str));
memset(temp.str,0,sizeof(temp.str));
a.len=0;
a.str[0]=map[x][y];
a.str[1]='\0';
if(a.len>anslen||(a.len==anslen&&strcmp(a.str,ans)>0))
{
strcpy(ans,a.str);
anslen=a.len;
}
intnum;
// num=atoi(a.str);
// vis[x][y][num]=1;
queue<structs>q;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
for(inti=0;i<2;i++)
{
if(!i)
{
temp.x=a.x;
temp.y=a.y+1;
strcpy(temp.str,a.str);
if(temp.x>=n||temp.y>=m)
continue;
if(map[temp.x][temp.y]>='0'&&map[temp.x][temp.y]<='9')
{
temp.len=a.len+1;
temp.str[temp.len]=map[temp.x][temp.y];
temp.str[temp.len+1]='\0';
// num=atoi(temp.str);
// if(!vis[temp.x][temp.y][num])
// {
if(temp.len>anslen||(temp.len==anslen&&strcmp(temp.str,ans)>0))
{
strcpy(ans,temp.str);
anslen=temp.len;
}
// vis[temp.x][temp.y][num]=1;
q.push(temp);
// }
}
}
else
{
temp.x=a.x+1;
temp.y=a.y;
strcpy(temp.str,a.str);
if(temp.x>=n||temp.y>=m)
continue;
if(map[temp.x][temp.y]>='0'&&map[temp.x][temp.y]<='9')
{
temp.len=a.len+1;
temp.str[temp.len]=map[temp.x][temp.y];
temp.str[temp.len+1]='\0';
// num=atoi(temp.str);
// if(!vis[temp.x][temp.y][num])
// {
if(temp.len>anslen||(temp.len==anslen&&strcmp(temp.str,ans)>0))
{
strcpy(ans,temp.str);
anslen=temp.len;
}
// vis[temp.x][temp.y][num]=1;
q.push(temp);
// }
}
}
}
}
}
intmain()
{
//intn,m;
while(scanf("%d%d",&m,&n)!=EOF,n||m)
{
inti,j;
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
scanf("%s",map[i]);
memset(ans,0,sizeof(ans));
ans[0]='0';
ans[1]='\0';
anslen=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]>='1'&&map[i][j]<='9')
bfs(i,j);
}
}
printf("%s\n",ans);
}
}
[/code]
相关文章推荐
- hdu 2222 AC自动机模板题(指针版+数组版)
- C/C++中各种类型int、long、double、char表示范围(最大最小值)
- linux中vmstat命令详解
- eclipse中outline中图标含义小结
- WR703N TTL刷机过程
- 用Python实现基本排序算法02——选择排序
- Android framework完整源码下载
- SQL数据库的升级过程—解决WMI服务无法链接过程
- rqnoj 389 心灵的抚慰 (floyd求解环的最小权值)
- 通过实例让你真正明白mapreduce---填空式、分布(分割)编程
- leetcode: Symmetric Tree
- N 的阶乘的另一种解法
- linux系统硬盘分区挂载及常用查看命令
- WPF中使用Image控件显示图片
- 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
- 分享MyEclipse安装及安装报错的解决方法
- LintCode-删除排序链表中的重复元素
- uploadPreview 上传图片预览
- Ajax实现跨域请求
- Storm-0.9.0.1安装部署 指导