Hoj 13326 Biggest Number

来源:互联网 发布:windows keynote 编辑:程序博客网 时间:2024/06/05 23:24

传送门: http://acm.hnu.cn/online/?action=problem&type=show&id=13326&courseid=0


Biggest NumberTime Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KBTotal submit users: 19, Accepted users: 7Problem 13326 : No special judgementProblem description

 


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 0
Sample Output
791452384
Problem 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