22 poj2935 Basic Wall Maze, poj2935maze
Each vertex can go in four directions. You can use vis [x] [y] [0 .. 3] indicates the direction of the vertex (x, y.
Then bfs is ready. The record path records the previous vertex of each node, and the recursive output can be done.
# Include <cstdio> # include <queue> # include <cstring> using namespace std; int dx [] = {0, 0, 1,-1 }; int dy [] = {1,-1, 0}; bool vis [7] [7] [4], vvis [7] [7]; char di [4] = {'s ', 'n', 'E', 'w'}; int sx, sy, ex, ey; struct node {int x, y, dir;} pre [10] [10]; void print (int xx, int yy) {if (xx = sx & yy = sy) return; print (pre [xx] [yy]. x, pre [xx] [yy]. y); printf ("% c", di [pre [xx] [yy]. dir]); // output the point direction} void bfs () {queue <node> q; memset (vvis, 0, sizeof Vvis); // mark this point without passing through vvis [sx] [sy] = 1; q. push (node) {sx, sy,-1}); // start with while (! Q. empty () {node now = q. front (); q. pop (); if (now. x = ex & now. y = ey) // output the question at the end to ensure that the solution {print (ex, ey); puts (""); return;} for (int I = 0; I <4; I ++) {if (! Vis [now. x] [now. y] [I]) // The direction can be {int xx = now. x + dx [I]; int yy = now. y + dy [I]; if (! Vvis [xx] [yy] & xx> 0 & xx <= 6 & yy> 0 & yy <= 6) // This point has not passed, and there is no cross-border {vvis [xx] [yy] = 1; pre [xx] [yy] = (node) {now. x, now. y, I}; q. push (node) {xx, yy, I}) ;}}} int main () {int I, j, a, B, c, d; while (scanf ("% d", & sx, & sy) & (sx | sy) {scanf ("% d", & ex, & ey); memset (vis, 0, sizeof vis); for (I = 0; I <3; I ++) {scanf ("% d", & a, & B, & c, & d); if (a = c) // horizontal wall {for (j = B + 1; j <= d; j ++) {if (! = 0) vis [a] [j] [2] = 1; if (! = 6) vis [a + 1] [j] [3] = 1 ;}} else // Vertical Wall {for (j = a + 1; j <= c; j ++) {if (B! = 0) vis [j] [B] [0] = 1; if (B! = 6) vis [j] [B + 1] [1] = 1 ;}} bfs ();} return 0 ;}