Poj 5025 saving Tang Monk (shape pressure search)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5025

Search question: Pay attention to the status of the Snake (the first pass to kill the snake for 2 s, the second time after 1 s ). The status is the coordinate, and the maximum value is added to the snake key. Use the coordinates to get the key of the highest level.

Code:

# Include <iostream> # include <queue> # include <map> # include <cstdio> # include <string> # include <cstring> # include <cmath> # include <algorithm> # define mod 1000000007 typedef long ll; using namespace STD; const int maxn = 105; char G [maxn] [maxn]; bool done [maxn] [maxn] [10]; int Sn [maxn] [maxn]; int DX [] = {0, 0, 1,-1}; int dy [] = {1,-1, 0}; int n, m, Sx, Sy, ex, ey; struct node {int X, Y, W, M, s; node (INT xx = 0, int YY = 0, int WW = 0, int mm = 0, int Ss = 0) // M indicates the maximum key number obtained, and s indicates the compression status of the snake ~ 2 ^ 5 {x = xx; y = YY; W = WW; M = mm; S = SS;} bool operator <(const node &) const {return W>. W ;}}; bool is_ill (int x, int y) {return x <0 | Y <0 | x> = n | Y> = N ;} int BFS () {priority_queue <node> q; memset (done, 0, sizeof done); q. push (node (sx, Sy, 0, 0); done [SX] [sy] [0] = 1; while (! Q. empty () {node E = Q. top (); q. pop (); // printf ("% d \ n", E. x, E. y, E. w, E. m, E. s); For (INT I = 0; I <4; I ++) {int curx = E. X + dx [I]; int Cury = E. Y + dy [I]; If (is_ill (curx, Cury) | G [curx] [Cury] = '#') continue; if (curx = ex & Cury = ey & E. m> = m) Return e. W + 1; if (G [curx] [Cury] = '. '&&! Done [curx] [Cury] [E. m]) {q. push (node (curx, Cury, E. W + 1, E. m, E. s); done [curx] [Cury] [E. m] = 1;} If (G [curx] [Cury] ='s '&&! Done [curx] [Cury] [E. m]) {If (E. S & Sn [curx] [Cury]) = 0) // the snake that has not killed this position {q. push (node (curx, Cury, E. W + 2, E. m, E. S | Sn [curx] [Cury]); done [curx] [Cury] [E. m] = 1;} else {q. push (node (curx, Cury, E. W + 1, E. m, E. s); done [curx] [Cury] [E. m] = 1 ;}} if (G [curx] [Cury]> = '0' & G [curx] [Cury] <= '9 ') {If (E. m + 1 = G [curx] [Cury]-'0 '&&! Done [curx] [Cury] [E. m + 1]) // get the next key {q. push (node (curx, Cury, E. W + 1, E. m + 1, E. s); done [curx] [Cury] [E. m + 1] = 1;} else if (! Done [curx] [Cury] [E. M]) // it is best to get the next one. If you cannot get it, just go over. (The key is too high or too low, and the key cannot be left blank (that is, the key is extended and not obtained at the same time) {q. push (node (curx, Cury, E. W + 1, E. m, E. s); done [curx] [Cury] [E. m] = 1 ;}}} return 0;} int main () {While (~ Scanf ("% d", & N, & M) & (N | M) {int CNT = 0; memset (Sn, 0, sizeof Sn ); for (INT I = 0; I <n; I ++) {scanf ("% s", G [I]); For (Int J = 0; j <N; j ++) if (G [I] [J] = 'k') SX = I, Sy = J, G [I] [J] = '. '; else if (G [I] [J] = 'T') EX = I, ey = J, G [I] [J] = '. '; else if (G [I] [J] = 's') Sn [I] [J] = 1 <(CNT ++ );} int ans = BFS (); If (ANS) printf ("% d \ n", ANS); else printf ("impossible \ n");} return 0 ;}

Saving Tang monk Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 149 accepted submission (s): 57


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
Recommendhujie


Poj 5025 saving Tang Monk (shape pressure search)

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.