Analysis: Status compression of BFS or DFS status + shortest path.
The former is similar to the method used to save the big soldier Ryan. The latter is very classic. the DFS is arranged in full and enumerated, and then added according to the shortest path.
Wa three times + TLE + AC.
Code:
Const NX: array [1 .. 4] of integer = (0, 0, 1,-1); NY: array [1 .. 4] of integer = (1,-1, 0); type JI = record X, Y, S, step: longint; end; var V: array [0 .. 1027, 0 .. 24, 0 .. 24] of Boolean; Q: array [0 .. 1000000] of JI; B: array [0 .. 25, 0 .. 25] of longint; A: array [0 .. 25, 0 .. 25] of char; sum, Head, tail, S, Y, x, t, step, now, n, m, I, J, K: longint; VV: Boolean; begin readln (m, n); while M <> 0 do begin fillchar (v, sizeof (V), 0); sum: =-1; for I: = 1 to n do begin for J: = 1 to M do begin read (A [I, j]); If a [I, j] = 'O' then begin X: = I; Y: = J; end else if a [I, j] = '*' Then begin Inc (SUM); B [I, j]: = sum; end; end; readln; end; If sum =-1 then begin writeln (0); readln (m, n); continue; end; VV: = false; head: = 1; tail: = 1; Q [1]. x: = x; Q [1]. y: = y; V [0, x, y]: = true; while head <= tail do begin X: = Q [head]. x; Y: = Q [head]. y; now: = Q [head]. s; Step: = Q [head]. step; for I: = 1 to 4 Do begin S: = x + NX [I]; T: = Y + ny [I]; If (S <1) or (T <1) or (S> N) or (T> m) then continue; if a [S, T] = 'X' then continue; if a [s, t] = '*' Then begin if V [now or (1 <B [S, T]), S, T] Then continue; Inc (tail ); Q [tail]. x: = s; Q [tail]. y: = T; Q [tail]. s: = now or (1 <B [S, T]); Q [tail]. step: = Step + 1; V [Q [tail]. s, S, T]: = true; if Q [tail]. S = 1 <(sum + 1)-1 then begin writeln (Step + 1); VV: = true; break; end; continue; end; If V [now, s, T] Then continue; V [now, S, T]: = true; Inc (tail); Q [tail]. s: = now; Q [tail]. x: = s; Q [tail]. y: = T; Q [tail]. step: = Step + 1; end; If VV then break; Inc (head); end; if not VV then writeln (-1); readln (m, n); end; end.