Topic Link https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=473
= = This problem is to give you a three-dimensional diagram, and then give you a starting point, an end point, the middle of the barrier = = ask you whether you can go from the beginning to the end, if the minimum number of steps is how many
Optimal solution = = with BFS, because it is three-dimensional, so use three-directional array to determine where to go next
#include <stdio.h>#include<string.h>#include<iostream>#include<queue>#include<algorithm>#defineN 33using namespacestd;intF[n][n][n];intx[6]={1,-1,0,0,0,0},y[6]={0,0,1,-1,0,0},z[6]={0,0,0,0,-1,1};structnode{intX,y,z,step;};intSx,sy,sz,ex,ey,ez;intn,m,k;intFlag;intBFs () {Queue<node>Q; Nodeinch, out; inch. X=SX,inch. Y=sy,inch. z=sz; F[SX][SY][SZ]=1; inch. step=0; Q.push (inch); while(!Q.empty ()) { inch=Q.front (); Q.pop (); if(inch.x==ex&&inch.y==ey&&inch. z==ez) {Flag=1; return inch. Step; } for(intI=0;i<6; i++) { out. x=inch. x+X[i]; out. y=inch. y+Y[i]; out. z=inch. z+Z[i]; out. step=inch. Step; if( out. x>=0&& out.x<n&& out. y>=0&& out.y<m&& out. z>=0&& out. z<k&&!f[ out. x][ out. y][ out. z]) {f[ out. x][ out. y][ out. z]=1; out. step++; Q.push ( out); } } }}intMain () { while(SCANF ("%d%d%d", &k,&n,&m)!=eof&&n&&m&&k) {flag=0; Chars[ -]; Memset (F,0,sizeof(f)); for(intI=0; i<k;i++) { for(intj=0; j<n;j++) {scanf ("%s", s); for(intL=0; l<m;l++) { if(s[l]=='S') {SX=J; Sy=l; SZ=i; } Else if(s[l]=='E') {ex=J; EY=l; EZ=i; } Else if(s[l]=='#') F[j][l][i]=1; } } } intans=BFS (); if(flag) printf ("escaped in%d minute (s). \ n", ans); Elseprintf ("trapped!\n"); } return 0;}
Uva532 (three-dimensional search)