您的位置:首页 > 其它

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.Theformatofeachdata
setisasfollows.
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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: