Biggest Number
来源:互联网 发布:vscode hexdump 编辑:程序博客网 时间:2024/05/20 10:22
原题描述:
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 thefour 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 andC (2<=R,C<=15, R*C<=30), the number of rows and columns of the maze. The nextR 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 withR=C=0, you should not process it.
Output
For each test case, print the biggest number you can find, on a single line.
样例输入
3 7##9784###123####45###0 0
样例输出
791452384
题目大意:就是输入一个矩形,里面包含有数字,只能沿着数字上下左右走(不能重复),一直到没办法再走的时候结束。要求输出走的路径,并且这个路径构成的数字最大。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[20][20];
int used[20][20]; //判断是否走过改路径
int ans[100]; //存放当前最大数数组
int ansl; //记录当前最大数位数
int aa[100]; //当前数
int zhan[100][2];
int used1[20][20]; //这个也是判断是否走过路径 下面代码中只用在一处地方
int neigh[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//查找上下左右位置用的数组
void search(int x,int y,int l,int z) //搜索函数
{
int i,j,top,bottom,xx,yy;
if(l>ansl || (l==ansl && z==1)) //如果该数大于最大数 z用来判断大小的 注意后面用法
{
memcpy(ans,aa,sizeof(ans)); //aa数组值复制到ans中去
ansl=l;
z=0;
}
memset(used1,0,sizeof(used1)); //初始化use1为0 即都没使用过
used1[x][y]=1;
top=0;bottom=1; //下面会用栈来存放后继结点的坐标 为的是记录当前数之后能构成的最长的数
zhan[0][0]=x;
zhan[0][1]=y;
while(top<bottom) //把后继结点都放入栈中 这里是提高速度的一核心
{
for(i=0;i<4;i++) //上下左右
{
xx=zhan[top][0]+neigh[i][0];
yy=zhan[top][1]+neigh[i][1];
if((xx>=0 && xx<r)&&(yy>=0 && yy<c)&& a[xx][yy]!='#' && used[xx][yy]==0 && used1[xx][yy]==0)
{
zhan[bottom][0]=xx;
zhan[bottom][1]=yy;
used1[xx][yy]=1;
bottom++;
}
}
top++;
}
if(l+top-1<ansl) //如果当前长度+后继结点构成最长数长度<最大数长度 直接返回
return;
if((l+top-1==ansl)&&(z==-1)) //长度相等但是比最长数小
return;
for(i=0;i<4;i++) //上下左右递归寻找下一个结点
{
xx=x+neigh[i][0];
yy=y+neigh[i][1];
if((xx>=0)&&(xx<r)&&(yy>=0)&&(yy<c)&&(a[xx][yy]!='#')&&(used[xx][yy]==0)) //下面都是search 注意z的值及其含义
{
aa[l]=a[xx][yy]-'0';
used[xx][yy]=1;
if(z!=0)
search(xx,yy,l+1);
else
if(l>=ansl)
{
z=1;
search(xx,yy,l+1);
z=0;
}
else
{
if(aa[l]>ans[l])
{
z=1;
search(xx,yy,l+1);
z=0;
}
else if(aa[l]==ans[l])
{
z=0;
search(xx,yy,l+1);
z=0;
}
else
{
z=-1;
search(xx,yy,l+1);
z=0;
}
}
used[xx][yy]=0;
}
}
}
int main()//把第一个结点单独拿出来而已 接下来的结点都是遍历过程
{
int i,j,r,c,z;
while(cin>>r>>c && r && c)
{
for(i=0;i<r;i++)
cin>>a[i];
memset(ans,0,sizeof(ans)); //当前最大数
ans[0]=-1;
ansl=1;
memset(aa,0,sizeof(aa)); //当前数
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]!='#')
{
used[i][j]=1; //该点已经经过
aa[0]=a[i][j]-'0'; //将字符转化为数字存入当前数组
if (a[i][j]-'0'>ans[0]) //与当前最大数比较
{
z=1;
search(i,j,1,z);
}
else if (a[i][j]-'0'==ans[0])
{
z=0;
search(i,j,1,z);
}
else
{
z=-1;
search(i,j,1,z);
}
used[i][j]=0;
}
}
}
for (i=0;i<ansl;i++)
cout<<ans[i];
cout<<endl;
}
return 0;
}
- Biggest Number
- UVA11882 Biggest Number
- 11882 - Biggest Number
- Hoj 13326 Biggest Number
- uva 11882Biggest Number
- UVA 11882Biggest Number
- toj 4610 Biggest Number
- uva Biggest Number UVA
- Biggest Number UVA
- Biggest Number UVA
- UVA11882 Biggest Number 强剪枝
- Uva 11882 Biggest Number (IDA*)
- UVA 11882(p218)----Biggest Number
- NBUT [1464] Biggest Number dfs
- 619. Biggest Single Number--MAX()
- 【搜索+强剪枝】UVA-11882 - Biggest Number
- Find Nth Biggest Number Based On QuickSort
- 11882 - Biggest Number(DFS + 剪枝)
- iOS7初体验(1)——第一个应用程序HelloWorld
- Oracle Merge 使用
- GPS 纠偏算法
- iframe跨域自适应高度
- 带有可变参数的宏(Macros with a Variable Number of Arguments)
- Biggest Number
- iOS如何让主界面不显示NavigationBar,而后面的有
- UIWebView之获取所点位置图片URL
- js前端排序
- 解决程序启动时mysql出现 unauthenticated user导致数据库处理慢的问题
- 集成struts2框架步骤
- 【单独编译使用WebRTC的音频处理模块 - android】
- 标准C++中的string类的用法总结
- mysql数据库主从同步