POJ 3984 maze (BFS), poj3984 maze bfs
Getting started with BFS, I did it for the first time.
# Include <iostream> # include <cstdio> # include <queue> using namespace std; int a [5] [5]; bool visit [5] [5]; int dx [4] = {0, 0,-1}; // Four Directions: 0: Right, 1: Bottom, 2: left, 3: Upper int dy [4] = {,-}; struct Node {int x; int y; int s; // path length int direc [30]; // record direction} node, next; bool judge (int x, int y) {if (x <0 | x> 4 | y <0 | y> 4) return true; if (visit [x] [y] = true | a [x] [y] = 1) return true; visit [x] [y] = true; // mark return false after access;} N Ode bfs () {queue <Node> q; visit [node. x] [node. y] = true; q. push (node); while (! Q. empty () {node = q. front (); if (node. x = 4 & node. y = 4) return node; q. pop (); for (int I = 0; I <4; I ++) // judge Four Directions {next = node; next. x = node. x + dx [I]; next. y = node. y + dy [I]; if (judge (next. x, next. y) continue; next. direc [node. s] = I; next. s = node. s + 1; q. push (next) ;}}int main () {// freopen ("in.txt", "r", stdin); for (int I = 0; I <5; I ++) for (int j = 0; j <5; j ++) scanf ("% d", & a [I] [j]); node ans = bfs (); int x = 0, y = 0; printf ("(0, 0) \ n"); for (int I = 0; I <ans. s; I ++) {x + = dx [ans. direc [I]; y + = dy [ans. direc [I]; printf ("(% d, % d) \ n", x, y);} return 0 ;}