AOJ-579-exam Transfer
Http://icpc.ahu.edu.cn/OJ/Problem.aspx? Id = 579
BFS, written by template, incorrect data input and processing during the game, ah... Too weak...
# Include <stdio. h> # include <string. h> # include <stdlib. h> int n, m; char map [105] [105]; int ans [105] [105]; int dir [4] [2] = }, {}, {0,-1}, {}; typedef struct {int x; int y;} node; int bfs (node s, node e) {node que [10005]; node tt; int head = 0, tail = 1, I; que [0] = s; ans [s. x] [s. y] = 0; map [s. x] [s. y] = '#'; while (head <tail) {s = que [head ++]; for (I = 0; I <4; I ++) {tt. x = s. x + dir [I] [0]; tt. y = s. y + dir [I] [1]; if (tt. x> = 0 & tt. x <n & tt. y> = 0 & tt. Y <m & map [tt. x] [tt. y]! = '#') {Ans [tt. x] [tt. y] = ans [s. x] [s. y] + 1; if (tt. x = e. x & tt. y = e. y) return ans [e. x] [e. y]; map [tt. x] [tt. y] = '#'; que [tail ++] = tt ;}} return 0 ;}int main () {int I, j; char ch; node s, e; int k1, k2; int ans; while (scanf ("% d", & n, & m )! = EOF) {memset (map, 0, sizeof (map); for (I = 0; I <n; I ++) {getchar (); for (j = 0; j <m; j ++) {scanf ("% c", & ch); if (ch = 'A') {s. x = I; s. y = j;} else if (ch = 'B') {e. x = I; e. y = j;} else if (ch = 'T') // The invigilator's lap is not reachable {for (k1 = I-1; k1 <= I + 1; k1 ++) for (k2 = J-1; k2 <= j + 1; k2 ++) if (k1> = 0 & k1 <n & k2> = 0 & k2 <m) map [k1] [k2] = '#';} if (map [I] [j]! = '#') Map [I] [j] = ch ;}} if (map [s. x] [s. y] = '#' | map [e. x] [e. y] = '#') {printf ("-1 \ n"); continue;} ans = bfs (s, e); if (ans) printf ("% d \ n", ans); elseprintf ("-1 \ n");} return 0 ;}