Hoj 13326 Biggest Number
来源:互联网 发布:windows keynote 编辑:程序博客网 时间:2024/06/05 23:24
传送门: http://acm.hnu.cn/online/?action=problem&type=show&id=13326&courseid=0
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
3 7##9784###123####45###0 0Sample Output
791452384Problem SourceHNU Contest
剪枝搜索,flag标记!!
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{ int x,y,l; node(int x_=0,int y_=0,int l_=0):x(x_),y(y_),l(l_){}};int R,C;char mapt[17][17];int ans[31],ansL;int aa[31];int vis[17][17];int mark[17][17];int flag;int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};queue<node> que;void dfs(int x,int y,int l){//当前这个点已经在aa数组里了。而l也是aa真实长度// cout<<"xyl: "<<x<<" "<<y<<" "<<l<<" flag: "<<flag<<" aa[] ";// for(int i=0;i<l;i++)cout<<aa[i]<<" ";cout<<endl; if(l>ansL||(l==ansL&&flag==1)){// cout<<"ansL: "<<ansL<<" "<<flag<<endl; memcpy(ans,aa,sizeof aa); ansL = l; flag=0; } que.push(node(x,y,0)); int deep=-1; memset(mark,0,sizeof mark); mark[x][y]=1; while(!que.empty()){ node tmp=que.front();que.pop(); deep++; for(int i=0;i<4;i++){ int dx=tmp.x+dir[i][0]; int dy=tmp.y+dir[i][1]; if(mapt[dx][dy]!='#'&&mark[dx][dy]==0&&vis[dx][dy]==0){ mark[dx][dy]=1; que.push(node(dx,dy,tmp.l+1)); } } }// cout<<deep<<" deep,ansL "<<ansL<<endl; if(l+deep<ansL||(l+deep==ansL&&flag==-1))return ; for(int i=0;i<4;i++){ int dx=x+dir[i][0]; int dy=y+dir[i][1]; if(mapt[dx][dy]!='#'&&vis[dx][dy]==0){ vis[dx][dy]=1; aa[l]=mapt[dx][dy]-'0'; if(flag==0){ if(aa[l]>ans[l]){ flag=1; dfs(dx,dy,l+1); flag=0; } else if(aa[l]==ans[l]){ dfs(dx,dy,l+1); } else if(aa[l]<ans[l]){ flag=-1; dfs(dx,dy,l+1); flag=0; } }else{ dfs(dx,dy,l+1); } vis[dx][dy]=0; } }}int main(){ #ifdef ACBang freopen("13326.in","r",stdin); #endif // ACBang while(scanf("%d%d",&R,&C),R||C){ for(int i=0;i<=R+1;i++)mapt[i][0]=mapt[i][C+1]='#'; for(int j=0;j<=C+1;j++)mapt[0][j]=mapt[R+1][j]='#'; for(int i=1;i<=R;i++){ char s[20];scanf("%s",s); for(int j=1;j<=C;j++) mapt[i][j]=s[j-1]; } memset(vis,0,sizeof vis);//并不需要每次dfs都清0,因为每次dfs后都恢复为0 ans[0]=-1,ansL=0; for(int i=1;i<=R;i++){ for(int j=1;j<=C;j++){ if(mapt[i][j]=='#')continue; aa[0]=mapt[i][j]-'0'; if(aa[0]>ans[0])flag=1; else if(aa[0]==ans[0])flag=0; else flag=-1; vis[i][j]=1; dfs(i,j,1); vis[i][j]=0;// for(int k=0;k<ansL;k++)printf("%d",ans[k]);puts("\n"); } } for(int i=0;i<ansL;i++)printf("%d",ans[i]);puts(""); } return 0;}
0 0
- Hoj 13326 Biggest Number
- Biggest Number
- UVA11882 Biggest Number
- 11882 - 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 + 剪枝)
- container-with-most-water
- linux获取MAC地址编程
- JAVA基础篇三(Java,C++中的异常机制)
- python operator — 标准函数操作
- Learning Python Day4
- Hoj 13326 Biggest Number
- 解释Activity的四种launchMode模式
- “稳定军心”对网站建设公司很重要
- printf的(i++)和(++i)详解(编译器不同有差异)
- Message MessageQueue Runnable Looper 和Handler的关系理解
- Myeclipse错误:Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project
- 南邮 OJ 1571 约瑟夫问题
- 记录下:Mysql导出表结构及表数据 mysqldump用法 - T00ls
- hdu 5348 MZL's endless loop 2015多校联合训练赛#5 找环+dfs