POJ 5025 Saving Tang Monk (shape pressure search), poj5025

Source: Internet
Author: User

POJ 5025 Saving Tang Monk (shape pressure search), poj5025

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. You do not need to remove the key of a snake because it is impossible to kill a snake and stay in the same place twice, but it is possible to take the key specially.

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





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.