http://acm.uestc.edu.cn/#/problem/show/1088
The maze of KingsTime limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
The king was trapped in a 3 -dimensional maze, and he wanted to escape the maze and go back to being genius, can you help him? Because the king is very kind, he quietly tells you that the subject reads into every line of the maze, to use scanf("%s"...)
...
Input
Multiple sets of test data, three integers for each set of test dataL,R,c ( 0<l,r,c≤30 ).
L represents the height of the maze, r and C represents rows and columns for each layer, respectively.
Next is L < Span id= "mathjax-span-949" class= "math" > rx Matrix of C , matrix containing 4 type characters ( S
, E
, .
, #
), S
represents the initial position of the king, E
represents the exit, #
represents the barrier. .
Where the representative can pass.
There is a blank line after each layer.
When L=R=C=0 , the input is interrupted.
Output
If you can escape the maze, output the shortest time in the following format:
Escaped in x minute(s).
(x means the shortest time to escape from the maze, and a faint clock to take one step)
Otherwise, the output:
Trapped!
Sample Input and output
Sample Input |
Sample Output |
3 4 5s.....###. ##.. ###.#############.####...###########.###### #E1 3 3s## #E # # #0 0 0 |
Escaped in minute (s). trapped! |
Puzzle: Three-dimensional maze, very simple BFS. At the beginning WA a pitch, and later found that because the OJ data is not standard, you can not use GetChar to eliminate whitespace characters, you have to use the%s filter whitespace. On the one hand some complain OJ data pit father, on the other hand summarizes the lesson, must have the courage to dare to doubt the correctness of the platform data, improve the robustness of their program.
Code:
1#include <fstream>2#include <iostream>3#include <cstdio>4#include <vector>5#include <cstdlib>6#include <cstring>7#include <algorithm>8#include <queue>9 Ten using namespacestd; One A structnode{ - intX,y,z,step; - }tmp; the - Const intn= *; - intl,r,c,x1,y1_,z1; -Queue<node>PQ; + CharA[n][n][n]; - BOOLB[n][n][n]; + intco[3][6]={{0,0,0,0,1,-1},{0,0,1,-1,0,0},{1,-1,0,0,0,0}}; A atInlineBOOLCheckintXintYintz); - intBFS (); - - intMain () - { - //freopen ("d:\\input.in", "R", stdin); in //freopen ("D:\\output.out", "w", stdout); - while(SCANF ("%d%d%d", &l,&r,&c) && (l|r|c)) { to BOOLdo{1; + for(intI=0; i<l;i++){ - for(intj=0; j<r;j++){ thescanf"%s", A[i][j]); * if(bo) $ for(intz=0; z<c;z++){Panax Notoginseng if(a[i][j][z]=='S'){ -x1=i,y1_=j,z1=Z; thedo{0; + Break; A } the } + } - } $ intans=BFS (); $ if(ans==-1) puts ("trapped!"); - Elseprintf"escaped in%d minute (s). \ n", ans); - } the return 0; - }WuyiInlineBOOLCheckintXintYintz) { the returnx>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&a[x][y][z]!='#'&&b[x][y][z]==0; - } Wu intBFs () { -memset (b,0,sizeof(b)); About while(!pq.empty ()) Pq.pop (); $tmp.step=0; -tmp.x=X1; -tmp.y=y1_; -tmp.z=Z1; A Pq.push (TMP); +b[x1][y1_][z1]=1; the while(!Pq.empty ()) { -tmp=Pq.front (); $ Pq.pop (); the intx=tmp.x,y=tmp.y,z=tmp.z; the inttx,ty,tz,ts=tmp.step+1; the for(intI=0;i<6; i++){ thetx=x+co[0][i]; -ty=y+co[1][i]; intz=z+co[2][i]; the if(check (Tx,ty,tz)) { theb[tx][ty][tz]=1; About if(a[tx][ty][tz]=='.'){ thetmp.x=tx,tmp.y=ty,tmp.z=tz,tmp.step=ts; the Pq.push (TMP); the}Else{ + returnts; - } the }Bayi } the } the return-1; -}
cdoj1088-King Maze (three-dimensional Maze shortest path) "BFS"