Stealing Harry Potter's precious
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1297 accepted submission (s): 619
Problem description Harry Potter has some precious. for example, his invisible robe, his wand and his owl. when Hogwarts School is in holiday, Harry Potter has to go back to Uncle Vernon's home. but he can't bring his precious with him. as you know, Uncle Vernon never allows such magic things in his house. so Harry has to deposit his precious in the Gringotts wizarding bank which is owned by some gobloud. the bank can be considered as a n × m grid consisting of n × m rooms. each room has a coordinate. the coordinates of the upper-left room is (1, 1), the down-Right room is (n, m) and the room below the upper-left room is (2, 1 )..... a 3 × 4 bank grid is shown below:
Some rooms are indestructible and some rooms are vulnerable. gobstmalways care more about their own safety than their MERs 'properties, so they live in the indestructible rooms and put MERs 'properties in vulnerable rooms. harry Potter's precious are also put in some vulnerable rooms. dudely wants to steal Harry's things this holiday. he gets the most advanced drilling machine from his father, Uncle Vernon, and drills into the bank. but he can only pass though the vulnerable rooms. he can't access the indestructible rooms. he starts from a certain vulnerable room, and then moves in four directions ctions: North, East, South and West. dudely knows where Harry's precious are. he wants to collect all Harry's precious by as less steps as possible. moving from one room to another adjacent room is called a 'step '. dudely doesn't want to get out of the bank before he collects all Harry's things. dudely is stupid. he pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Input there are several test cases.
In each test cases:
The first line are two integers n and M, meaning that the bank is a n x m grid (0 <n, m <= 100 ).
Then a n × M matrix follows. each element is a letter standing for a room. '# 'means a indestructible room ,'. 'means a vulnerable room, And the only' @ 'means the vulnerable room from which dudely starts to move.
The next line is an integer K (0 <k <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers x and y, meaning that there is a precious in room (x, y ).
The input ends with n = 0 and m = 0
Output for each test case, print the minimum number of steps dudely must take. If dudely can't get all Harry's things, print-1.
Sample Input
2 3##@#.#12 24 4#@##....####....22 12 40 0
Sample output
-15
Idea: Use a three-dimensional array to record the status of some treasures, and store the shortest time after some treasures are obtained.
# Include <iostream> # include <stdio. h> # include <string. h >#include <algorithm> # include <queue> # include <vector> using namespace STD; # define N 105 # define M 30000 const int INF = (INT) 1e8; char G [N] [N]; int a [n] [N]; // records the serial number of each treasure. Int mark [N] [N] [32]; int K, n, M, L, ans; int dir [4] [2] = {, 0,-1,-,}; struct node {int x, y, s, T; // status and time int num; // Number of treasures friend bool operator <(node A, Node B) {return. t> B. t ;}}; bool judge (in T x, int y) {If (x <0 | x> = n | Y <0 | Y> = M | G [x] [Y] = '#') return true; return false;} void Inti () // initializes the array. The time is infinite {int I, j, k; for (I = 0; I <N; I ++) {for (j = 0; j <m; j ++) {for (k = 0; k <L; k ++) {mark [I] [J] [k] = inf ;}}} void BFS (int x, int y) {int I, Di, DJ; priority_queue <node> q; node cur, next; cur. T = cur. S = cur. num = 0; cur. X = x; cur. y = y; Mark [x] [y] [0] = 0; q. push (cur); While (! Q. empty () {cur = Q. top (); q. pop (); If (next. num = k) {ans = min (ANS, next. t); continue;} for (I = 0; I <4; I ++) {next. X = di = cur. X + dir [I] [0]; next. y = Dj = cur. Y + dir [I] [1]; next. T = cur. t + 1; next. S = cur. s; next. num = cur. num; If (Judge (Di, DJ) continue; int T = A [di] [DJ]; If (T! =-1 & (next. S & (1 <t) = 0) {next. S | = (1 <t); next. num ++; If (next. num = k) {ans = min (ANS, next. t); Return ;}} if (MARK [di] [DJ] [next. s]> next. t) {mark [di] [DJ] [next. s] = next. t; q. push (next) ;}}} int main () {int I, j, X, Y; int Si, SJ; while (scanf ("% d ", & N, & M), N | M) {for (I = 0; I <n; I ++) {scanf ("% s ", G [I]); For (j = 0; j <m; j ++) {If (G [I] [J] = '@') {Si = I; SJ = J ;}} scanf ("% d", & K); memset (A,-1, sizeof ()); for (I = 0, L = 1; I <K; I ++) {scanf ("% d", & X, & Y ); A [x-1] [Y-1] = I; L = L <1; // up to 4 treasures, so the maximum state is 1111 ,} // L is the total number of States plus one Inti (); ans = inf; BFS (Si, SJ); If (ANS = inf) ans =-1; printf ("% d \ n", ANS);} return 0 ;}
HDU 4771 stealing Harry Potter's precious (State compression + BFS)