Topic Links:
UVA11882
Problem Solving Ideas:
The conventional idea is to enumerate each point, violent Dfs, and then select the largest one but the topic is only given 1000MS and this needs pruning.
Pruning 1:
Assuming the current answer is ans, when we go to a point (x, y), BFS determines the number of squares that can be contacted. It's best to assume that we can get to the point where we can go from (x, y) now. Set from (x, y) can walk to the points of maxlen, then if from the starting point to (x, y) through the lattice, plus maxlen, there is no ans length, then no matter from (x, y) How to search, we can not replace our current ans (long is the King understand), then direct backtracking, Stop this point. This pruning effect is still good.
And if it equals, we need a flag flag to indicate the size relationship of the current string and the ANS string.
Pruning 2: If the ANS string is already the maximum possible length and the starting point of the search is less than the start of the ANS string, skip directly
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <time.h > #define PRINTIME printf ("%.3lf\n", (double) clock ()/clocks_per_sec) #define MAXN 33using namespace Std;struct node{ int x, y;} Que[10050],u;char ans[maxn],temp[maxn];int Dir[4][2]={1,0,-1,0,0,1,0,-1};char Map[maxn][maxn];char G[MAXN][MAXN]; int sum;int max_len;int n,m;int ok (int x,int y) {if (x<1| | y<1| | x>n| | Y>M) return 0; return 1;} int bfs (int x,int y) {for (int i=1;i<=n;i++) strcpy (g[i]+1,map[i]+1); int head=0,tail=0; int tx,ty; Que[tail++]= (node) {x, y}; while (Head<tail) {u=que[head++]; for (int i=0;i<4;i++) {tx=u.x+dir[i][0]; TY=U.Y+DIR[I][1]; if (!ok (tx,ty) | | g[tx][ty]== ' # ') continue; g[tx][ty]= ' # '; Que[tail++]= (node) {tx,ty}; }} return head; int flag;void dfs (int x,int y,int len) {if (len>max_len| | (len==max_len&Amp;&flag==1)) {Max_len=len; temp[len]= ' + '; strcpy (ans,temp); flag=0; A starting point to get the new ANS string needs to continue to compare}//printf ("x=%d y=%d len=%d flag=%d\n", X,y,len,flag); int Res=bfs (x, y); if (len+res-1<max_len| | (Len+res-1==max_len&&flag==-1)) Pruning 1 return; int tx,ty; for (int i=0;i<4;i++) {tx=x+dir[i][0]; TY=Y+DIR[I][1]; if (!ok (tx,ty) | | map[tx][ty]== ' # ') continue; Temp[len]=map[tx][ty]; map[tx][ty]= ' # '; if (flag==0) {if (Temp[len]<ans[len]) flag=-1; else if (Temp[len]>ans[len]) flag=1; DFS (TX,TY,LEN+1); flag=0; Backtracking} else Dfs (tx,ty,len+1); Map[tx][ty]=temp[len]; }}int Main () {//Freopen ("In.txt", "R", stdin); while (scanf ("%d%d", &n,&m) && (m+n)) {sum=0; max_len=0; memset (ans, ' + ', sizeof (ans)); memset (TEMP, ' n ', sizeof (temp)); for (int i=1;i<=n;i++) {scanf ("%s", map[i]+1); for (int j=1;j<=m;j++) if (map[i][j]!= ' # ') sum++; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {if (map[i][j]!= ' # ') {if (max_len==sum&&map[i][j]<ans[0])//pruning 2 continue; TEMP[0]=MAP[I][J]; map[i][j]= ' # '; if (ans[0]==temp[0]) flag=0; else if (temp[0]>ans[0]) flag=1; else Flag=-1; DFS (i,j,1); MAP[I][J]=TEMP[0]; }} printf ("%s\n", ans);//Printime; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA11882 biggest number Strong pruning