[ACM] HDU 5025 saving Tang Monk (State compression, BFS)

Source: Internet
Author: User
Saving Tang monk

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 941 accepted submission (s): 352


Problem description Journey to the West (also monkey) is one of the four great classical novels of Chinese literature. it was written by Wu Cheng 'en during the Ming dynasty. in this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. most of demons wanted to eat Tang monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. so, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon white bones. white Bones lived in a palace and she cuffed Tang monk in a room. sun Wukong managed to get into the palace. but to rescue Tang monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. each character stands for a room. in the matrix, 'K' represents the original position of Sun Wukong, 't'represents the location of Tang monk and 'ss' stands for a room with a snake in it. please note that there are only one 'K' and one 'T', and at most five snkes in the palace. and ,'. 'means a clear room as well' # 'means a deadly room which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the room, but there is at most one key in one room. there are at most 9 kinds of keys. A room with a key in it is represented by a digit (from '1' to '9 '). for example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. to save Tang monk, Sun Wukong must get all kinds of keys (in other words, at least one key for each kind ).

For each step, Sun Wukong cocould move to the adjacent rooms (could t deadly rooms) in 4 directions ctions (north, west, south and east), and each step took him one minute. if he entered a room in which a living snake stayed, he must kill the snake. killing a snake also took one minute. if Sun Wukong entered a room where there is a key of kind N, Sun wocould get that key if and only if he had already got keys of kind 1, kind 2... and kind N-1. in other words, Sun Wukong must get a key of kind n before he cocould get a key of kind n + 1 (n> = 1 ). if Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. if Sun Wukong didn't get enough keys, he still cocould pass through Tang Monk's room. since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. please figure out the minimum time Sun Wukong needed to rescue Tang monk.


 

Inputthere are several test cases.

For each case, the first line between des two integers n and M (0 <n <= 100, 0 <= m <= 9 ), meaning that the palace is a n × n matrix and Sun Wukong needed M kinds of keys (kind 1, kind 2 ,... kind m ).

Then the n × n matrix follows.

The input ends with n = 0 and m = 0.


 

Outputfor each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang monk. if it's impossible for Sun Wukong to complete the mission, print "impossible" (No quotes ).


 

Sample Input
3 1K.S##11#T3 1K#T.S#1#.3 2K#T.S.21.0 0
 


 

Sample output
5impossible8
 


 

Source2014 ACM/ICPC Asia Regional Guangzhou online

 

 

Solution:

The question is a map. 'K' represents the position of Sun Wukong, that is, the starting point. 'T' represents the position of Tang Miao, And the number '1' '2' represents several keys, 'S' indicates a snake. '#' cannot be taken. The purpose of the question is that Sun Wukong can save Tang Miao. The premise is that he can only get the given m key to save Tang Miao, except for the '#' position, all other locations can be taken. (If Tang Miao's position is reached, but he does not have the given m key, the task cannot be completed, must get the M Clock Key). It takes one more minute to kill the snake when arriving at the 's' location. It takes one minute to take another location and ask how many minutes it will take to save Tang Miao. If not, output impossible.

It is really hard to understand the meaning of the question: the only way to save Tang Monk is to get the keys of all kinds, and then reach the Tang Monk's position to save. Sun Wukong and Tang Miao can be used multiple times.

You also need to pay special attention to the status of snakes. When the first time the snake is killed and the second time it arrives at this position, you do not need to kill the snake any more. Then, you can add a number to the snake, set the number to 1, and set the number to 0, status compression.

Define f [I] [J] [k] [State] to go to coordinate I. J. The K key has been obtained. The current state of the snake is state. Search for breadth first.

Reference: http://www.cnblogs.com/whatbeg/p/3983522.html

Code:

# Include <iostream> # include <algorithm> # include <string. h >#include <map >#include <queue> using namespace STD; const int INF = 0x3f3f3f; int n, m; char MP [102] [102]; int DX [4] = {,-}; int dy [4] = {1,-, 0 }; int f [102] [102] [12] [35]; // F [I] [J] [k] [State] is coordinate I, the minimum number of steps int Sx, Sy when the K key is obtained at the position J and the state when the snake is killed is State; // The starting position int SCNT; // The number of snakes int ans; map <pair <int, int>, int> snake; // ID of the snake at a certain position. struct node {int X, Y, K, S, starts from 1, step;}; queu E <node> q; void Init () {memset (F, INF, sizeof (f); snake. clear (); SCNT = 0; ans = inf; For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {CIN> MP [I] [J]; If (MP [I] [J] = 'k') SX = I, Sy = J, MP [I] [J] = '. '; // do not forget MP [I] [J] = '. 'This sentence can also be repeated multiple times at the starting position using else if (MP [I] [J] = 's') Snake [make_pair (I, j)] = ++ SCNT; // No.} f [SX] [sy] [0] [0] = 0;} bool OK (INT X, int y) {If (x> = 1 & x <= N & Y> = 1 & Y <= N & MP [x] [Y]! = '#') Return true; return false;} void BFS (node st) {If (! Q. Empty () Q. Pop (); q. Push (ST); While (! Q. empty () {node cur = Q. front (); q. pop (); node next; // the next state node for (INT I = 0; I <4; I ++) {int newx = cur. X + dx [I]; int newy = cur. Y + dy [I]; If (! OK (newx, newy) continue; next. X = newx, next. y = newy; If (MP [newx] [newy] = 's') {int th = snake [make_pair (newx, newy)]; If (cur. S & (1 <(TH-1) // The Snake has been killed {next. S = cur. s; next. k = cur. k; next. step = cur. step + 1;} else {next. S = (cur. S | (1 <(TH-1); next. k = cur. k; next. step = cur. step + 2;} If (next. step <F [newx] [newy] [next. k] [next. s]) {f [newx] [newy] [next. k] [next. s] = next. step; q. push (next) ;}} else if (MP [newx] [newy] >='1' & MP [newx] [newy] <= '9 ') {int th = MP [newx] [newy]-'0'; If (Th = cur. k + 1) // The current key is exactly what you want. The existing key is added with 1 next. k = cur. k + 1; else next. k = cur. k; next. S = cur. s; next. step = cur. step + 1; if (next. step <F [newx] [newy] [next. k] [next. s]) {f [newx] [newy] [next. k] [next. s] = next. step; q. push (next) ;}} else if (MP [newx] [newy] = '. ') {next. k = cur. k; next. S = cur. s; next. step = cur. step + 1; if (next. step <F [newx] [newy] [next. k] [next. s]) {f [newx] [newy] [next. k] [next. s] = next. step; q. push (next) ;}} else if (MP [newx] [newy] = 'T') {next. k = cur. k; next. S = cur. s; next. step = cur. step + 1; if (next. step <F [newx] [newy] [next. k] [next. s]) f [newx] [newy] [next. k] [next. s] = next. step; If (next. k = m) // ans = min (F [newx] [newy] [next. k] [next. s], ANS); else Q. push (next) ;}}} int main () {While (CIN> N> M & (N | M) {Init (); node temp; temp. X = Sx, temp. y = Sy, temp. k = 0, temp. S = 0, temp. step = 0; BFS (temp); If (ANS = inf) cout <"impossible" <Endl; else cout <ans <Endl;} return 0 ;}


 

[ACM] HDU 5025 saving Tang Monk (State compression, 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.