Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3681
Bytes, BFs + binary + DFS.
At first, I was too lazy and wanted to pass this question directly by BFs. As a result, TLE still wanted to use pruning or TLE.
Helpless, use the second method to think of, the distance between the BFS pre-processing point and the point, then the length of the binary enumeration, DFS query can escape.
Wa is not clear at the time of writing.
So I wrote a random script, and then ran it violently. Finally, I found an error.
... One type of data cannot pass...
3 3
GDF
DDS
YSS
The reason is very simple. It indicates the distance between two points. I set it to-1 during preprocessing...
That is, if there is no path between two points, then the distance between them is-1...
So step f to G is-1... G to Y is also step-1...
As you can imagine, steps from F to y are-2...
# Include <cstdio> # include <cstring> int dir [32] [32]; int y, G, n, m; char MP [16] [16]; int RMP [16] [16], RX, Ry; struct point {int X, Y; point () {} Point (int xx, int YY) {x = xx; y = YY ;}} que [5000]; const int FX [] = {, 1,-1}; const int FY [] = {1, 0}; void BFS (int x, int y) {int vis [16] [16]; int S, T, K; point No, nx; memset (VIS, -1, sizeof (VIS); que [S = T = 0] = point (x, y); vis [x] [Y] = 0; while (S <= T) {No = que [s ++]; If (MP [No. x] [No. y]! ='S ') {dir [RMP [x] [Y] [RMP [No. x] [No. y] = vis [No. x] [No. y] ;}for (k = 0; k <4; k ++) {NX. X = No. X + FX [k]; NX. y = No. Y + FY [k]; If (nx. x <0 | NX. Y <0 | NX. x> = n | NX. y> = m) continue; If (vis [NX. x] [NX. y]! =-1 | MP [NX. x] [NX. y] = 'D') continue; vis [NX. x] [NX. y] = vis [No. x] [No. y] + 1; que [++ T] = NX ;}} int has [31]; int DFS (int x, int now, int Len, int num) {If (num = Y-1) return 1; for (INT I = 1; I <Y; I ++) {If (now + dir [x] [I] <= Len & has [I] = 0 & dir [x] [I]! =-1) {has [I] = 1; if (DFS (I, now + dir [x] [I], Len, num + 1) return 1; has [I] = 0 ;}}for (INT I = 30; I> G; I --) {If (now + dir [x] [I] <= Len & has [I] = 0 & dir [x] [I]! =-1) {has [I] = 1; if (DFS (I, 0, Len, num) return 1; has [I] = 0 ;}} return 0;} int bitfind () {int L = 0, r = 55000, mid; while (L <= r) {mid = (L + r)> 1; memset (has, 0, sizeof (has); has [0] = 1; if (DFS (0, 0, mid, 0) {r = mid-1 ;} else {L = Mid + 1 ;}} return l ;}int never () {int I; for (I = 1; I <Y; I ++) {If (dir [0] [I] =-1) {return 1 ;}} return 0 ;}void Init () {int I, j; y = 1; G = 30; for (I = 0; I <n; I ++) {scanf ("% s", MP [I]); For (j = 0; j <m; j ++) {If (MP [I] [J] = 'F') {RX = I; ry = J; RMP [I] [J] = 0;} else if (MP [I] [J] = 'y') {RMP [I] [J] = y ++ ;} else if (MP [I] [J] = 'G') {RMP [I] [J] = g --;} else if (MP [I] [J] = 'D') {RMP [I] [J] =-1 ;} else {RMP [I] [J] = 0 ;}} memset (Dir,-1, sizeof (DIR); for (I = 0; I <N; I ++) {for (j = 0; j <m; j ++) {If (RMP [I] [J]> 0 | MP [I] [J] = 'F') {BFS (I, j ); /* // output the preprocessing result, that is, the number of steps from (I, j) to (II, JJ. Printf ("--- % d, % d \ n", I, j); For (INT II = 0; II <n; II ++) {for (int jj = 0; JJ <m; JJ ++) {If (MP [II] [JJ]! = 'D' & MP [II] [JJ]! ='S ') {printf ("-> % d, % d ..... % d \ n ", II, JJ, dir [RMP [I] [J] [RMP [II] [JJ]) ;}} */}}} int main () {While (~ Scanf ("% d", & N, & M) {If (n = 0) break; Init (); If (never ()) {puts ("-1") ;}else {printf ("% d \ n", bitfind () ;}return 0 ;}/ * 7 7ssssssdfsgdsgdssdsddssggsssgsssssssssssssdgsddysdss */