HDU 5025 saving Tang monk -- BFS

Source: Internet
Author: User

Question: Give a map. Sun Wukong (k) saves Tang Monk (T). The map's indicates a snake. It takes up to five dead snakes a minute, '1 '~ 'M' indicates M keys (M <= 9). Sun Wukong needs to get these M keys in sequence before saving Tang Miao. Before gathering M keys, he can pass Tang Miao, A collection of X keys can pass through x + 1, x + 2 .. key, ask how many steps to save Tang Miao.

Solution: BFs. Each node maintains four values:

X, Y: Current Coordinate

Key: Key keys have been collected.

Step: How many steps have been taken

S: The access status of the snake. (2 ^ 5 indicates that the number of snakes has been killed. If 1 is set to 1, the snake has been killed)

Then, take Tang Seng as the key m + 1, and then add some optimizations:

To avoid timeout, a global dis [x] [y] [Key] [s] is used to represent (x, y), and keys have been collected, the minimum number of steps when the access status of the snake is S. If the number of steps in the current status is> = dis [Current Status] When BFS is expanded, it will not be extended.

The logic in BFS is simple. Check the code.

Finally, enumerate the status s of the snake, and take the minimum value of DIS [x] [y] [M + 1] [s] as the minimum number of steps.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> # include <queue> # include <map> # define INF 0x3f3f3fusing namespace STD; # define n 1000007int dis [104] [104] [12] [33], stot, M; struct node {int X, Y, key, step, s ;}; int DX [4] = {0, 0, 1,-1}; int dy [4] = {1,-1, 0}; Map <pair <int, int>, int> snake; char ss [105] [105]; int n, m; bool OK (INT NX, int NY) {If (nx <n && NX> = 0 & ny <n & ny> = 0 & SS [NX] [NY]! = '#') Return true; return false;} void BFS (node s) {queue <node> que; Que. Push (s); While (! Que. empty () {node now = que. front (); que. pop (); int Nx = now. x, NY = now. y; int key = now. key, step = now. step; int S = now. s; node TMP; For (int K = 0; k <4; k ++) {int kx = NX + dx [k]; int Ky = ny + dy [k]; If (! OK (kx, KY) continue; TMP. X = kx, TMP. y = ky; If (ss [kx] [Ky] = 's') // snake {int ind = snake [make_pair (kx, KY)]; // The first snake TMP. key = key; If (S & (1 <(ind-1) // If {TMP has been killed. S = s; TMP. step = Step + 1;} else // otherwise, kill {TMP. S = S | (1 <(ind-1); TMP. step = Step + 2;} If (TMP. step <dis [kx] [Ky] [TMP. key] [TMP. s]) {dis [kx] [Ky] [TMP. key] [TMP. s] = TMP. step; que. push (TMP) ;}} else if (ss [kx] [Ky] >='1' & SS [kx] [Ky] <=' 9 ') // key point {int num = ss [kx] [Ky]-'0'; TMP. step = Step + 1; TMP. S = s; If (num = Key + 1) // exactly the TMP to be taken. key = Key + 1; else TMP. key = key; If (TMP. step <dis [kx] [Ky] [TMP. key] [TMP. s]) {dis [kx] [Ky] [TMP. key] [TMP. s] = TMP. step; que. push (TMP) ;}} else if (ss [kx] [Ky] = '$') // Tang Seng this point {TMP. key = key; TMP. S = s; TMP. step = Step + 1; if (M = Key + 1) // all the keys have been collected and will not be extended, update DIS to get dis [kx] [Ky] [m] [s] = min (DIS [kx] [Ky] [m] [S], step + 1); else // No integration, continue with que. push (TMP);} else if (ss [kx] [Ky] = '. ') {TMP. key = key; TMP. S = s; TMP. step = Step + 1; if (TMP. step <dis [kx] [Ky] [TMP. key] [TMP. s]) {dis [kx] [Ky] [TMP. key] [TMP. s] = TMP. step; que. push (TMP) ;}}}} int main () {int Sx, ex, Sy, ey; int I, j; while (scanf ("% d ", & N, & M )! = EOF & N + M) {If (n = 1) {puts ("impossible"); continue;} snake. clear (); stot = 0; M = m + 1; for (I = 0; I <n; I ++) {scanf ("% s ", ss [I]); For (j = 0; j <n; j ++) {If (ss [I] [J] = 'k') SX = I, sy = J, ss [I] [J] = '. '; else if (ss [I] [J] = 'T') EX = I, ey = J, ss [I] [J] =' $ '; else if (ss [I] [J] ='s ') Snake [make_pair (I, j)] = ++ stot ;}} node TMP; TMP. X = Sx, TMP. y = Sy, TMP. key = 0, TMP. step = 0, TMP. S = 0; memset (DIS, INF, sizeof (DIS); DIS [SX] [sy] [0] [0] = 0; BFS (TMP ); int mini = inf; for (I = 0; I <(1 <stot); I ++) mini = min (Mini, dis [Ex] [ey] [m] [I]); If (mini = inf) puts ("impossible"); else printf ("% d \ n ", mini);} return 0 ;}
View code

 

HDU 5025 saving Tang monk -- BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.