After debugging for a while, we found thatProgramYes, the test data is wrong-the data read in the previous question is ==||||
The old saying goes, "Be careful, students !"
Simple BFs.
/** Tyvj-1074 warrior demeanor ox * Mike-W * 2011-10-26 * ----------------------- * BFS */# include <stdio. h> # include <stdlib. h> # include <string. h> # define size 155 # define qsize 50000 # define submittypedef struct _ POS {int X, Y;} Pos; char f [size] [size]; int G [size] [size]; pos q [qsize]; int head, tail, Len; int row, Col; int xx1, xx2, YY1, yy2; int d [8] [2] = {-1,-2}, {1,-2}, {2,-1}, {2, 1}, {1, 2 }, {-1}, {-2}, {-2,-1}; int push (int x, I NT y) {q [tail]. X = x; Q [tail]. y = y; If (++ tail = qsize) tail = 0; Len ++; return 0;} int POP (int * X, int * Y) {* x = Q [head]. x; * Y = Q [head]. y; If (++ head = qsize) Head = 0; Len --; return 0 ;}# ifdef dispint disp (void) {int I, J; for (I = 0; I <row; I ++) for (j = 0; j <Col; j ++) printf ("% d % C ", G [I] [J], (J + 1 = col )? '\ N': ''); Return 0 ;}# endifint solve (void) {int X, Y, XX, YY, I; push (xx1, YY1 ); G [xx1] [YY1] = 1; while (LEN> 0) {Pop (& X, & Y); for (I = 0; I <8; I ++) {xx = x + d [I] [0]; YY = Y + d [I] [1]; if (XX> = 0 & XX <row & YY> = 0 & YY <Col & F [XX] [YY] = '. '&&! G [XX] [YY]) {push (XX, YY); G [XX] [YY] = G [x] [Y] + 1; if (xx = xx2 & YY = yy2) {Len = 0; break ;}}# ifdef dispdisp (); # endif} return G [xx2] [yy2]-1;} int main (void) {int I, j; # ifndef submitfreopen ("in", "r ", stdin); # endifscanf ("% d", & Col, & Row); for (I = 0; I <row; I ++) scanf ("% s", F [I]); for (I = 0; I <row; I ++) for (j = 0; j <Col; j ++) {If (F [I] [J] = 'H') xx1 = I, YY1 = J; else if (F [I] [J] = 'k') xx2 = I, yy2 = J;} f [xx2] [yy2] = '. '; printf ("% d \ n", solve (); Return 0 ;}