#include <iostream> #include <queue> #include <memory.h> using namespace std; int l, r, c; char maze[35][35][35]; int next_step[6][3] = { { -1, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, { 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 } }; struct coordinate { int z, x, y; int step;
<span class="hl-meta hl-method hl-constructor hl-c++"><span class="hl-entity hl-name hl-function hl-constructor hl-c++">coordinate</span></span><span class="hl-meta hl-method hl-parameters hl-c++"><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span></span></span><span class="hl-meta hl-method hl-parameters hl-c++"><span class="hl-meta hl-group hl-c++"><span class="hl-storage hl-type hl-c">int</span> <span class="hl-variable hl-parameter hl-c++">in_z</span><span class="hl-punctuation hl-separator hl-c++">,</span> <span class="hl-storage hl-type hl-c">int</span> <span class="hl-variable hl-parameter hl-c++">in_x</span><span class="hl-punctuation hl-separator hl-c++">,</span> <span class="hl-storage hl-type hl-c">int</span> <span class="hl-variable hl-parameter hl-c++">in_y</span><span class="hl-punctuation hl-separator hl-c++">,</span> <span class="hl-storage hl-type hl-c">int</span> <span class="hl-variable hl-parameter hl-c++">in_step</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span></span></span><span class="hl-meta hl-method hl-c++"> </span><span class="hl-meta hl-method hl-c++"><span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span></span></span><span class="hl-meta hl-method hl-c++"><span class="hl-meta hl-block hl-c++"> z <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> in_z<span class="hl-punctuation hl-terminator hl-c++">;</span> x <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> in_x<span class="hl-punctuation hl-terminator hl-c++">;</span> y <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> in_y<span class="hl-punctuation hl-terminator hl-c++">;</span> step <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> in_step<span class="hl-punctuation hl-terminator hl-c++">;</span> </span></span><span class="hl-meta hl-method hl-c++"><span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span></span>
}; int bfs(int z, int x, int y) { queue<coordinate>q; coordinate current(z, x, y, 0); q.push(current); int step; while (!q.empty()) { z = q.front().z; x = q.front().x; y = q.front().y; step = q.front().step; maze[z][x][y] = '#'; for (int s = 0; s < 6; s++) { if (((z + next_step[s][0]) >= 0) && ((z + next_step[s][0]) < l) && ((x + next_step[s][1]) >= 0) && ((x + next_step[s][1]) < r) && ((y + next_step[s][2]) >= 0) && ((y + next_step[s][2]) < c)) { coordinate next(z + next_step[s][0], x + next_step[s][1], y + next_step[s][2], step + 1); if (maze[next.z][next.x][next.y] == 'E') { return next.step; }
<span class="hl-keyword hl-control hl-c++">if</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span>maze<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">z</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">x</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">y</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-comparison hl-c">==</span> <span class="hl-string hl-quoted hl-single hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">'</span>.<span class="hl-punctuation hl-definition hl-string hl-end hl-c">'</span></span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> <span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span> q<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-meta hl-method-call hl-c++"><span class="hl-variable hl-function hl-member hl-c++">push</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span></span></span><span class="hl-meta hl-method-call hl-c++"><span class="hl-meta hl-group hl-c++">next</span></span><span class="hl-meta hl-method-call hl-c++"><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span></span><span class="hl-punctuation hl-terminator hl-c++">;</span> maze<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">z</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">x</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>next<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-variable hl-other hl-readwrite hl-member hl-c++">y</span><span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> <span class="hl-string hl-quoted hl-single hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">'</span>#<span class="hl-punctuation hl-definition hl-string hl-end hl-c">'</span></span><span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> q<span class="hl-punctuation hl-accessor hl-dot hl-c++">.</span><span class="hl-meta hl-method-call hl-c++"><span class="hl-variable hl-function hl-member hl-c++">pop</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span></span></span><span class="hl-meta hl-method-call hl-c++"></span><span class="hl-meta hl-method-call hl-c++"><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span></span><span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-keyword hl-control hl-c++">return</span> <span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>
}
int main() { while (1) { cin>>l>>r>>c; if (l == 0 && r == 0 && c == 0) break; memset(maze, '#', sizeof(maze));
<span class="hl-storage hl-type hl-c">int</span> x<span class="hl-punctuation hl-separator hl-c++">,</span> y<span class="hl-punctuation hl-separator hl-c++">,</span> z<span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-keyword hl-control hl-c++">for</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span><span class="hl-storage hl-type hl-c">int</span> i<span class="hl-keyword hl-operator hl-assignment hl-c">=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-comparison hl-c"><</span>l<span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> <span class="hl-keyword hl-control hl-c++">for</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span><span class="hl-storage hl-type hl-c">int</span> j<span class="hl-keyword hl-operator hl-assignment hl-c">=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>j<span class="hl-keyword hl-operator hl-comparison hl-c"><</span>r<span class="hl-punctuation hl-terminator hl-c++">;</span>j<span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> <span class="hl-keyword hl-control hl-c++">for</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span><span class="hl-storage hl-type hl-c">int</span> k<span class="hl-keyword hl-operator hl-assignment hl-c">=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>k<span class="hl-keyword hl-operator hl-comparison hl-c"><</span>c<span class="hl-punctuation hl-terminator hl-c++">;</span>k<span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> <span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span> cin<span class="hl-keyword hl-operator hl-arithmetic hl-c">>></span>maze<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>j<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>k<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-keyword hl-control hl-c++">if</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span>maze<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>j<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>k<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span><span class="hl-keyword hl-operator hl-comparison hl-c">==</span><span class="hl-string hl-quoted hl-single hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">'</span>S<span class="hl-punctuation hl-definition hl-string hl-end hl-c">'</span></span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> <span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span> z<span class="hl-keyword hl-operator hl-assignment hl-c">=</span>i<span class="hl-punctuation hl-separator hl-c++">,</span> x<span class="hl-keyword hl-operator hl-assignment hl-c">=</span>j<span class="hl-punctuation hl-separator hl-c++">,</span> y<span class="hl-keyword hl-operator hl-assignment hl-c">=</span>k<span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-storage hl-type hl-c">int</span> shortest_time <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> <span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span> shortest_time <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> <span class="hl-meta hl-function-call hl-c++"><span class="hl-variable hl-function hl-c++">bfs</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span></span></span><span class="hl-meta hl-function-call hl-c++"><span class="hl-meta hl-group hl-c++">z<span class="hl-punctuation hl-separator hl-c++">,</span> x<span class="hl-punctuation hl-separator hl-c++">,</span> y</span></span><span class="hl-meta hl-function-call hl-c++"><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span></span><span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-keyword hl-control hl-c++">if</span> <span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span>shortest_time<span class="hl-keyword hl-operator hl-comparison hl-c">!=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span> cout<span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span><span class="hl-string hl-quoted hl-double hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">"</span>Escaped in <span class="hl-punctuation hl-definition hl-string hl-end hl-c">"</span></span><span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span>shortest_time<span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span><span class="hl-string hl-quoted hl-double hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">"</span> minute(s).<span class="hl-punctuation hl-definition hl-string hl-end hl-c">"</span></span><span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span>endl<span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-keyword hl-control hl-c++">else</span> cout<span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span><span class="hl-string hl-quoted hl-double hl-c"><span class="hl-punctuation hl-definition hl-string hl-begin hl-c">"</span>Trapped!<span class="hl-punctuation hl-definition hl-string hl-end hl-c">"</span></span><span class="hl-keyword hl-operator hl-arithmetic hl-c"><<</span>endl<span class="hl-punctuation hl-terminator hl-c++">;</span> <span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span> <span class="hl-keyword hl-control hl-c++">return</span> <span class="hl-constant hl-numeric hl-c++">1</span><span class="hl-punctuation hl-terminator hl-c++">;</span>
}