[Title Description] (Source: Byvoid wow simulation stage. 3 P4)
"Our latest research result, the poison research plan, was stolen by the hateful sindiga spy !" As a member of lavenode, you must I was shocked because it was the result of a year by our cutting-edge researchers. It was obtained by sindiga, and we may have a disaster recovery. The cunning sindiga did not bring the poison Research Scheme back to Whitehorse fort, but hid it. But after all It is our skill. We have discovered their hiding place by purchasing the latest research result of gnomiters, the electrostatic radiation detector. Original They have already built a huge city in the ground of the otrange, and now they have put the poison research plan at the most Deep. The better news is that we have found the entrance to the underground city. As a good thief, you must learn the same way and apply others. -- Steal the poison research plan. However, sindiga has provided rigorous defense, and, even worse, they have bought electromagnetic monitors from the ground. No matter how clever your stealth skills are, as soon as you get close to it, an alarm is triggered. Only by damaging its power supply system can the electromagnetic monitor be quiet Silent messages are invalid.
Now, the electrostatic radiation detector has generated a map for us, which tells you the layout structure of the entire underground city, including The position of an electromagnetic Monitor and the position of its power supply device. The underground city of sindiga can be described as an n * N table, city entry In ( 1 , 1 ), And the target protocol for research on poisons is at (N, N. Each cell may be an empty space, an obstacle, and a singles' guard. Shi, an electromagnetic monitor, or a power supply device. From the entrance, you can only move up, down, left, right to the adjacent Cell, cannot stay in the same place. You can only enter the open space, or lose the location of the electromagnetic monitor of the power supply system, or destroy the power supply device. You cannot move on to an obstacle, nor go to the sight of the xindiga guard. Xindiga guard can monitor the cells in which he is located and the upper, lower, and lower sides. A total of five positions, and their line of sight can overlap. You cannot kill or be found by any of them. Supply of each electromagnetic Monitor The electric device may exist, cannot be damaged, or does not exist at all. One power supply device may also correspond to zero, one or more electromagnetic monitors, This means that all corresponding electromagnetic monitors will be destroyed. ( 1 , 1 ) And (N, N) must be passable. Lavenode requires you to execute Before the task, we should first provide a plan, that is, to determine at least how many steps are required to obtain our poison research plan.
[Input format]
The second row contains two integers, N and M. The map size is N. * N, the number of power supply devices is M.
The 2 - N + 1 Line, N integers in each line, each integer I may be 0 , - 1 , - 2 Or a positive integer. I = 0 Indicates this bit Set For a blank space, I =- 1 This position is an obstacle, I =- 2 Indicates that this location is a sindiga guard. If I is [ 1 , M] is a positive integer, it indicates that the position is a power supply device, its number is I. If I is a positive integer greater than m, it indicates the position For an electromagnetic monitor, its electricity supply is - The M power supply device is provided. [Output format]
An integer that represents the minimum number of steps required to obtain the protocol.
[Example input]
6 2
0 0 0 - 2 - 1 2
- 1 0 0 0 - 1 0
- 2 0 0 0 3 3
- 2 0 0 - 1 - 1 4
0 - 1 0 0 - 1 0
1 0 0 0 - 1 0
[Sample output]
24
[Example]
For example, map S is the entrance, T is the target, and black cells are obstacles. Each e Indicates a guard, and (e) indicates a guard. Monitoring range. K1 indicates Power Supply Device 1 , K2 indicates the Power Supply Device 2 . D1 indicates the electromagnetic monitor with the power supply device 1, D2 indicates the supply The electric device is an electromagnetic Monitor of 2.
The optimal route is ( 1 , 1 ) → ( 1 , 2 ) → ( 2 , 2 ) → ( 2 , 3 ) → ( 3 , 3 ) → ( 4 , 3 ) → ( 5 , 3 ) → ( 6 , 3 ) → ( 6 , 2 ) → ( 6 , 1 ) (Damage Power Supply 1) → ( 6 , 2 ) → ( 6 , 3 ) → ( 5 , 3 ) → ( 4 , 3 ) → ( 3 , 3 ) → ( 3 , 4 ) → ( 3 , 5 ) → ( 3 , 6 ) → ( 2 , 6 ) → ( 1 , 6 ) (Damage Power Supply 2 ) → ( 2 , 6 ) → ( 3 , 6 ) → ( 4.6 ) → ( 5 , 6 ) → ( 6 , 6 )
[Data scale]
1 <= N <= 50
0 <= M <= 16
This is a key-opening search question. The general idea is to open the record array with one-dimensional space records.Status when a key is foundAnd then take several keys as one layer for BFs. In this case, you can use bitwise operations to hash the key.
Handle various obstacles. If it is a soldier, mark both the top, bottom, and left directions.
ReferenceCode:
Program syndicate; const DX: array [1 .. 4] of integer = (0, 0,-1, 1); DY: array [1 .. 4] of integer = (1,-1, 0); Type L = record X, Y, Dist: integer; ft: array [1 .. 16] of Boolean; end; var map: array [-1 .. 51,-1 .. 51] of integer; FF: array [1 .. 16] of Boolean; V: array [0 .. 100000, 0 .. 50, 0 .. 50] of Boolean; // 3D tag array n, m, I, j: longint; F, S, XX, YY: longint; D: array [1 .. 1000000] of L; K: longint; begin readln (n, m); for I: = 1 to n do for J: = 1 to n do begin read (k ); if K =-2 then // process soldiers begin map [I, j]: =-2; Map [I + 1, J]: =-2; Map [I-1, j]: =-2; Map [I, J-1]: =-2; Map [I, j + 1]: =-2; end; If map [I, j] <>-2 then map [I, j]: = K; end; F: = 1; s: = 1; d [1]. x: = 1; d [1]. y: = 1; fillchar (v, sizeof (V), false); V [0, 1]: = true; while F <= s do begin for I: = 1 to 4 Do begin XX: = d [f]. X + dx [I]; YY: = d [f]. Y + dy [I]; K: = 0; for J: = 1 to M do begin FF [J]: = d [f]. FT [J]; If FF [J] Then K: = k + 1 <(J-1); // bit operation records the key end; If (XX> 0) and (XX <= N) and (yy> 0) and (yy <= N) then begin if (Map [XX, YY]> 0) and (Map [XX, YY] <= m) then if not V [K, XX, YY] Then begin V [K, XX, YY]: = true; Inc (s); K: = k + 1 <(Map [XX, YY]-1); if S> 1000000 then s: = 1; FF [map [XX, YY]: = true; d [s]. FT: = ff; D [s]. dist: = d [f]. dist + 1; d [s]. x: = xx; d [s]. y: = YY; V [K, XX, YY]: = true; end; If map [XX, YY]> M then if (FF [map [XX, YY, YY]-M]) and (not V [K, XX, YY]) then begin V [K, XX, YY]: = true; Inc (s ); if S> 1000000 then s: = 1; d [s]. FT: = ff; D [s]. dist: = d [f]. dist + 1; d [s]. x: = xx; d [s]. y: = YY; end; If map [XX, YY] = 0 then if not V [K, XX, YY] Then begin V [K, XX, YY]: = true; Inc (s); if S> 1000000 then s: = 1; d [s]. FT: = ff; D [s]. dist: = d [f]. dist + 1; d [s]. x: = xx; d [s]. y: = YY; end; If (xx = N) and (yy = N) then begin writeln (d [s]. dist); close (input); close (output); halt; end; Inc (f); if F> 1000000 then F: = 1; end; writeln ('HTTP: // www.cnblogs.com/saltless'); end.
Address: http://www.cnblogs.com/saltless/archive/2010/11/15/1877353.html
(Saltless original, reprinted please indicate the source)