Daily Punch-In (2/2)
Click to open link
Main topic:
A three-dimensional maze, given the starting point and end point, to find the shortest path.
PS: The length and width of the height are not more than 30
Idea 1:
First think of Dfs, only the first time to write three-dimensional DFS, feeling and two-dimensional nothing different.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib
> #include <algorithm> using namespace std;
const int INF = 1E8+7;
const int MAXN = 35;
BOOL VIS[MAXN][MAXN][MAXN];
int N,K,M,SX,SY,SZ,FX,FY,FZ;
int A[MAXN][MAXN][MAXN];
Long Long Ans,tot;
int dx[6]={1,0,0,0,0,-1};
int dy[6]={0,1,0,0,-1,0};
int dz[6]={0,0,1,-1,0,0};
void Dfs (int x,int y,int z) {if (X==FX&&Y==FY&&Z==FZ) {ans = min (ans,tot);
Return
} if (Ans!=inf&&tot>ans) return;
for (int i=0;i<6;i++) {int newx = x+dx[i];
int newy = Y+dy[i];
int newz = Z+dz[i]; if (newx>=0&&newx<n&&newy>=0&&newy<k&&newz>=0&&newz<m
&&!vis[newx][newy][newz]&&a[newx][newy][newz]) {VIS[NEWX][NEWY][NEWZ] = 1;
tot++;
DFS (NEWX,NEWY,NEWZ);
tot--;
VIS[NEWX][NEWY][NEWZ] = 0; }}} int main () {while (~scanf ("%d%d%d", &n,&k,&m) &&N&&AMp;k&&m) {memset (a,0,sizeof (a));
memset (vis,0,sizeof (VIS));
ans = INF;
tot = 0;
for (int i=0;i<n;i++) for (int j=0;j<k;j++) for (int l=0;l<m;l++) {char s;
cin>>s;
if (s== ' # ') a[i][j][l] = 0;
else if (s== '. ') a[i][j][l] = 1;
else if (s== ' s ') {sx = I,sy = J,sz = l;
A[i][j][l] = 0;
} else if (s== ' E ') {fx = I,fy = J,FZ = l;
A[I][J][L] = 1;
}} dfs (SX,SY,SZ); if (ans==inf) {cout<< "trapped!"
<<endl; } else cout<< "escaped in" <<ans<< "minute (s)."
<<endl;
} return 0; }
The first time I write the experience of the morning, cut a branch first. If the current TOT value has exceeded the previous optimal solution, then pass. Last time also encountered such a problem, so pruning is still tle. Sure enough, T. At this time there is a pruning idea, that is, every step to record the best solution to the current lattice, do not meet the direct pass, unfortunately, I will not write.
Idea two:
After DFS fails, naturally think of BFS.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib
> #include <algorithm> #include <queue> using namespace std;
const int INF = 1E8+7;
const int MAXN = 35;
struct node{int x,y,z,tot;};
BOOL VIS[MAXN][MAXN][MAXN];
int N,K,M,SX,SY,SZ,FX,FY,FZ;
int FLAG[MAXN][MAXN][MAXN];
Long long ans;
int dx[6]={1,0,0,0,0,-1};
int dy[6]={0,1,0,0,-1,0};
int dz[6]={0,0,1,-1,0,0};
int BFs () {Node A;
Queue<node> Q;
a.x = SX;
A.Y = sy;
A.Z = sz;
A.tot = 0;
Q.push (a);
while (!q.empty ()) {a = Q.front ();
Q.pop ();
if (A.X==FX&&A.Y==FY&&A.Z==FZ) return a.tot;
for (int i=0;i<6;i++) {node Next;
Next.x = A.x+dx[i];
Next.y = A.y+dy[i];
Next.z = A.z+dz[i]; if (next.x>=0&&next.x<n&&next.y>=0&&next.y<k&&next.z>=0& &next.z<m&&!vis[next.x][next.y][next.z]&&flag[next.x][next.y][next.z]) {Next.tot = a.tot+1;
Q.push (next);
Vis[next.x][next.y][next.z]=1;
}}} return 0; } int main () {while (~scanf ("%d%d%d", &n,&k,&m) &&n&&k&&m) {memset (vis,0,sizeof (
VIS));
for (int i=0;i<n;i++) for (int j=0;j<k;j++) for (int l=0;l<m;l++) {char s;
cin>>s;
if (s== ' # ') flag[i][j][l] = 0;
else if (s== '. ') flag[i][j][l] = 1;
else if (s== ' s ') {sx = I,sy = J,sz = l;
Flag[i][j][l] = 0;
} else if (s== ' E ') {fx = I,fy = J,FZ = l;
FLAG[I][J][L] = 1;
}} ans = BFs (); if (ans==0) {cout<< "trapped!"
<<endl; } else cout<< "escaped in" <<ans<< "minute (s)."
<<endl;
} return 0; }
Because of the optimality of wide search, the first solution must be the optimal solution, so the answer can be directly output.
In the wide search process, I usually do not use the VIS array to mark, but this time tle. Draw from the last program design contest C buy ring experience, added a vis array, sure enough a.
In order to Blue Bridge Cup, the current stage of the practice of violent search mainly