The seventh question of Exercise G

Source: Internet
Author: User

Test instructions

Give a case, want you to start from a certain point, always go to the limit (that is, can not be extended), then you go through the point will be connected to a number, the different way of course there will be different numbers, the requirement is the maximum number of output (note that each block walk once can not go again)

Ideas

1. Enumerate all the points as the starting point, and then ask for the maximum number of points that can be obtained from this point. 2. Then the maximum number that can be reached from one point using DFS is timed out only by using DFS.

Therefore, it is necessary to optimize pruning

DFS is the process of building and traversing the solution tree, in the deep-first search there are some forks can be judged without traversing, then you can skip this part, continue off

Pruning: First a number it is the biggest difference with another number is the length, even if it is the largest two digits, I am the smallest three digits, I am still greater than it, so you can use BFS, use it to determine the maximum length from this point, if this length is less than the maximum length I have retained, then no more search, Skip directly.

Another situation is that the number of BFS out of the length and the maximum number of previous reservations are equal? Now that the BFS has been used for pre-judgment, it is better to record the number of the longest distances that have arrived from that point. Then compare this number with the maximum number of reservations, and if it's small, then don't go one step further.

#include"Iostream"#include"CString"#include"Queue"#include"Ctype.h"#include"Algorithm"UsingNamespace Std;Char A[33][33];int can[33];int book[33][33];int Book2[33][33];int mN;int NEX[4][2]={{0,1},{1,0},{-1,0},{0,-1}};int TX, Ty;boolCmp(int a,int b){Return a>b;}typedefstruct Node{int No[33], Len;voidInit(){len=0;}booloperator<(ConstNode &rhs)Const{If(len! = RHS. len)Return len< RHS. len;For(int I=0; I< Len;++i)If*n-[I]! = RHS, N-[I])Return No[I]< RHS, N-[I];ReturnFalse;}}node;NodeAns, now;IntBFs(int x,int y){Queue<int>Que; Que.Push(x*33+ y);int F=1; Can[0]= A[x][y]-' 0 ';Memset(Book2,0,sizeof(Book2)); Book2[x][y]=1;While(!que.Empty()){int tmp= que.Front(); Que.Pop();int NX= tmp/33, NY= tmp%33;For(int I=0; I<4;++i){int px= NX+ NEX[I][0], py= NY+ NEX[I][1];If(!IsDigit(A[px][py])|| Book[px][py]|| Book2[px][py])Continue; Book2[px][py]=1; Can[F++]= A[px][py]-' 0 '; Que.Push(px*33+ py);}}return F;}voidDfs(int x,int y){Now, N-[Now. len++]= A[x][y]-' 0 '; Book[x][y]=1;For(int I=0; I<4;++i){int px= X+nex[I][0], py= y+ NEX[I][1];If(!IsDigit(A[px][py])|| Book[px][py])Continue;int Wantlen=BFs(px, py);If(now. len+ Wantlen< ans. len)Continue;If(now. len+ Wantlen= = ans. len){Sort(Can, can+ Wantlen);NodeTmp= Now;For(int I= Wantlen-1; I>=0;-I.) TMP, N-[TMP. len++]= Can[I];If(TMP< ans)Continue;}Dfs(px, py);}If(ans< now) ans= Now;--now. len; Book[x][y]=False;}IntMain(){While(CIN>>m>>n&&m){Memset(A,0,sizeof(A));Memset(Book,0,sizeof(Book));For(int I=0; I<m; I++)scanf('%s 'A[I]); ans.Init(); now.Init();For(Int J=0; j<m; j++)For(int k=0; k<n; k++)If(IsDigit(A[j][k))  dfs   (J  ,k   for   ( int kk  =  0  ;kk  <ans  .len  ;kk  ++) cout  <<ans  .no  [kk  ];cout  << Endl     return  0      

Seventh Exercise G problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.