Links: http://vjudge.net/problem/23314
Analysis: Two kinds of pruning schemes.
1. Find out the number of points that can be reached at the current location (although the number of points actually can be reached is smaller than it, but optimistic estimates can be), if the current number plus the number is still less than the current optimal solution length, then pruning.
2. If the number can be reached from small to Dalian after the current number, if still less than the current optimal solution, then pruning. (Find out all the points that can still be reached with the BFS implementation (optimistic estimate)).
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5#include <cstring>6#include <queue>7 using namespacestd;8 9 Const intMAXN = the+5;Ten One intN, M, v[ -]; A stringans; - CharMAZE[MAXN][MAXN]; - BOOLVIS[MAXN][MAXN], MARK[MAXN][MAXN]; the - BOOLSmallerstringnum) { - if(Num.size () > Ans.size ())return true; - Else if(num.size () = = Ans.size () && num > Ans)return true; + Else return false; - } + A Const intdx[4] = {1, -1,0,0}; at Const intdy[4] = {0,0,1, -1}; - intBFsintXinty) { -queue<pair<int,int> >Q; -memset (Mark,0,sizeof(Mark)); - Q.push (Make_pair (x, y)); -Mark[x][y] =true; in intCNT =0; - while(!Q.empty ()) { topair<int,int> U =Q.front (); Q.pop (); + for(intDIR =0; Dir <4; dir++) { - intNX = U.first + Dx[dir], NY = U.second +Dy[dir]; the if(NX <1|| NX > N | | NY <1|| NY > M)Continue; * if(Maze[nx][ny] = ='#'|| Vis[nx][ny] | | Mark[nx][ny])Continue; $Mark[nx][ny] =true;Panax Notoginsengv[cnt++] = Maze[nx][ny]-'0'; - Q.push (Make_pair (NX, NY)); the } + } A returnCNT; the } + - voidDfsintXintYstringnum) { $ if(smaller (num)) ans =num; $ Else { - intCNT =BFS (x, y); - if(Num.size () + CNT < ans.size ())return; the if(Num.size () + cnt = =ans.size ()) { -Sort (V, v +CNT);Wuyi stringnum2 =num; the for(inti = cnt-1; I >=0; i--) -Num2 + =Char(V[i] +'0'); Wu if(!smaller (NUM2))return; - } About } $ for(intDIR =0; Dir <4; dir++) { - intNX = x + Dx[dir], NY = y +Dy[dir]; - if(NX <1|| NX > N | | NY <1|| NY > M)Continue; - if(Maze[nx][ny] = ='#')Continue; A if(Vis[nx][ny])Continue;ElseVis[nx][ny] =true; +DFS (NX, NY, NUM +Maze[nx][ny]); theVis[nx][ny] =false; - } $ } the thevoidsolve () { the for(inti =1; I <= N; i++) the for(intj =1; J <= M; J + +) { - if(Maze[i][j] = ='#')Continue; inmemset (Vis,false,sizeof(Vis)); theVIS[I][J] =true; the stringnum =""; Aboutnum + =Maze[i][j]; the Dfs (i, J, num); the } thecout << ans <<Endl; + } - the intMain () {Bayi while(Cin >> n >> M &&N) { the for(inti =1; I <= N; i++) scanf ("%s", Maze[i] +1); the ans.clear (); solve (); - } - return 0; the}
UVA11882 biggest number (dfs+ pruning)