Get the start point and end point to find the path with the least Holes
The normal BFS () is enough. I want to say, oh, my god, I forgot to clear the queue. I went crazy about Wa During the simulation competition.
# Include <iostream> # include <algorithm> # include <queue> using namespace STD; int vis [1001] [1001], n, m, Si, SJ, EI, EJ, ans; char G [1001] [1001]; int dir [4] [2] = {}, {0,-1 }, {-1, 0 }}; struct state {int X, Y, CNT; State (INT _ x = 0, int _ y = 0, int _ CNT = 0 ): X (_ x), y (_ y), CNT (_ CNT) {}; friend bool operator <(const State & A, const State & B) {return. CNT> B. CNT ;}; priority_queue <State> q; void BFS () {q. push (State (Si, SJ, 0); vis [Si] [SJ] = 0; State TMP; while (! Q. empty () {TMP = Q. top (); q. pop (); If (TMP. X = EI & TMP. y = EJ) {ans = TMP. CNT; return ;}for (int K = 0; k <4; k ++) {int I = TMP. X + dir [k] [0]; Int J = TMP. Y + dir [k] [1]; int t; if (I> N | I <1 | j> M | j <1) continue; if (G [I] [J] = 'X') t = TMP. CNT; else t = TMP. CNT + 1; if (vis [I] [J]> T) {vis [I] [J] = T; q. push (State (I, j, t) ;}}} int main () {While (scanf ("% d", & N, & M) = 2 & (N | M) {getchar (); For (INT I = 1; I <= N; I ++) {gets (G [I] + 1); For (Int J = 1; j <= m; j ++) {// Scanf ("% C", & G [I] [J]); vis [I] [J] = int_max ;}} scanf ("% d", & Si, & SJ); scanf ("% d", & EI, & EJ); ans =-1; while (! Q. Empty () Q. Pop (); BFS (); printf ("% d \ n", ANS);} return 0 ;}