HDU 2128 tempter of the bone II (BFS + difficult to judge)

Source: Internet
Author: User
Tempter of the bone II

Time Limit: 10000/5000 MS (Java/others) memory limit: 98304/32768 K (Java/Others)

Total submission (s): 1128 accepted submission (s): 282 problem descriptionthe doggie found a bone in an unsupported ent maze, which fascinated him a lot. however, when he picked it up, the maze was changed and the way he came in was lost. he realized that the bone was a trap, and he tried desperately to get out
Of this maze.

The maze was a rectangle with the sizes of N by M. the maze is made up of a door, drawing walland drawing explosives. doggie need to reach the door to escape from the tempter. in every second, he cocould move one block to one of the upper, lower, left or right neighboring
Blocks. and if the destination is a wall, doggie need another second explode and a explosive to explode it if he had some explosives. once he entered a block with explosives, he can take away all of the explosives. can the Poor doggie keep ve? Please help him.

Inputthe input consists of multiple test cases. the first line of each test case contains two integers n, m, (2 <= n, m <= 8 ). which denote the sizes of the maze. the next n lines give the maze layout, with each line containing M characters.
A character is one of the following:

'X': a block of wall;
'S ': the start point of the doggie;
'D': the door;
'.': An empty block;
'1' -- '9': Explosives in that block.

Note, initially he had no explosives.

The input is terminated with two 0's. This test case is not to be processed.

Outputfor each test case, print the minimum time the doggie need to escape if the doggie can have ve, or-1 otherwise.

Sample Input

4 4SX..XX......1..D4 4S.X1......XX..XD0 0
 

Sample output

-19
 

Authorxhd

Sourcehdu 2007-10 Programming Contest
Feeling: All other things about this question are simple, that is, it is hard to judge and think about it. In the past, I used vector <node> vis [maxn] [maxn] to solve the problem. A little slow, and it ran to Ms. PS: a lot of code on the internet is wrong, because Hangzhou Power's data is watery, so it's over. This code is tight. In a matrix, a start point and an end point are given. In the matrix, some have walls and some have bombs. For a wall, it takes 1 s to take one step, it takes one second for each door to be blown up, asking the shortest time from the start point to the end point.

Thought: Because the map time is changing, it is generally difficult to judge. I handled it like this: I used two long records to record the Stateb state and the statew state of the wall when I got to the current position. If I went to the same point again, check whether the status of STB and STW at this time is exactly the same as that before. If yes, the STB and STW do not need to be added to the stack. If no, the STB and STW will be added to the stack. In this way, we can ensure that all the statuses are pushed to the stack. Although some of them are invalid, it is difficult to judge which ones are invalid. Therefore, all the statuses are pushed to the stack for processing.
PS: idea comes from: http://blog.csdn.net/acm_cxlove/article/details/7635497
Code:

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <queue> # include <vector> # define maxn 10 using namespace STD; int n, m, ans; int Sx, Sy, ex, ey; long STW, STB, curstw, curstb; int MP [maxn] [maxn]; int DX [] = {-1, 1, 0, 0}; int dy [] = {0, 0,-1, 1}; char s [maxn]; struct node {int X, Y; int step, bomb; long statew; long Stateb; friend bool operator <(const node & XX, const node & YY ){ Return xx. step> yy. step ;}} cur, now; vector <node> vis [maxn] [maxn]; // Position + struct judge weight priority_queue <node> q; bool isok (INT Tx, int ty) // judge the weight {int I, j, SZ; SZ = vis [TX] [ty]. size (); for (I = 0; I <SZ; I ++) {If (vis [TX] [ty] [I]. stateb = curstb & Vis [TX] [ty] [I]. statew = curstw) return false;} return true;} bool BFS () {int I, j, T, NX, NY, nstep, nbomb, TX, Ty; long long nstw, NSTB, tstw, tstb; while (! Q. empty () Q. pop (); for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {vis [I] [J]. clear () ;}} cur. X = SX; cur. y = sy; cur. step = 0; cur. bomb = 0; cur. stateb = STB; cur. statew = STW; vis [SX] [sy]. push_back (cur); q. push (cur); While (! Q. empty () {now = Q. top (); q. pop (); Nx = now. x; ny = now. y; nstep = now. step; nbomb = now. bomb; NSTB = now. stateb; nstw = now. statew; for (I = 0; I <4; I ++) {Tx = NX + dx [I]; ty = ny + dy [I]; t = (tx-1) * m + ty-1; If (TX <1 | TX> N | ty <1 | ty> m) continue; if (nbomb = 0 & (nstw & (1ll <t) continue; // The Wall curstb = NSTB; curstw = nstw; cur. bomb = nbomb; If (NSTB & (1ll <t) curstb = NSTB &~ (1ll <t ); // if a bomb exists at this location, the attacker cannot process the bomb. Because the MP is changing, the attacker must process the current STB and STW. If (nstw & (1ll <t )) curstw = nstw &~ (1ll <t); // if the position is a wall, the wall is blown up if (isok (TX, Ty) {cur. X = TX; cur. y = ty; If (Tx = ex & ty = ey) // determine the end point here. It's faster and start to judge TLE outside {ans = nstep + 1; return true;} cur. bomb = nbomb; If (NSTB & (1ll <t) cur. bomb + = MP [TX] [ty]; If (nstw & (1ll <t) cur. bomb --; cur. stateb = curstb; cur. statew = curstw; cur. step = nstep + 1; if (nstw & (1ll <t) cur. step ++; vis [TX] [ty]. push_back (cur); q. push (cur) ;}}return false;} int main () {int I, j, T; while (scanf ("% d", & N, & M), N | M) {memset (MP, 0, sizeof (MP); STW = STB = 0; for (I = 1; I <= N; I ++) {scanf ("% s", S); For (j = 1; j <= m; j ++) {T = (I-1) * m + J-1; If (s [J-1] = 's') SX = I, Sy = J; else if (s [J-1] = 'D ') ex = I, ey = J; else if (s [J-1] = 'X') {MP [I] [J] =-1; STW = (1ll <t) | STW;} else if (s [J-1]> = '1' & S [J-1] <= '9 ') {MP [I] [J] = s [J-1]-'0'; STB = (1ll <t) | STB ;}} if (BFS ()) printf ("% d \ n", ANS); else printf ("-1 \ n");} return 0;}/* 5 5s1xx .. XX .. xx.1 .... XX... xd6 5s. xx1x. 1x1xx. x. xxxxxxxxxxxxxdx2 6s. 1xxd1 .. xxx4 4s1x1xxxxxxdxxxxx6 2s1 .. 1xxxxxdx8 8 ...... XD. XXXX .. x. XXXXX... XXXXXXX. xxxxxxx... 4xxxxxxxxxxx3xxxxxxxsans: 141798938 */

Code 2: Process bombs and walls at the same time, which is doubled to 1400 ms ++. When a bomb does not exist, the state can be viewed as a wall. After it is acquired, it becomes a blank space. Therefore, the map only has two states: walking and not leaving. Only one long can represent its State. This kind of processing is also rigorous. Why?

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <queue> # include <vector> # define maxn 10 using namespace STD; int n, m, ans; int Sx, Sy, ex, ey; long STW, curstw; int MP [maxn] [maxn]; int DX [] = {-1, 1, 0, 0}; int dy [] = {0,-}; char s [maxn]; struct node {int X, Y; int step, bomb; long statew; friend bool operator <(const node & XX, const node & YY) {return xx. step> yy. step;} Cu R, now; vector <node> vis [maxn] [maxn]; // Position + struct judge weight priority_queue <node> q; bool isok (INT Tx, int ty) // weight {int I, j, SZ; SZ = vis [TX] [ty]. size (); for (I = 0; I <SZ; I ++) {If (vis [TX] [ty] [I]. statew = curstw) return false;} return true;} bool BFS () {int I, j, T, NX, NY, nstep, nbomb, TX, Ty; long long nstw, tstw; while (! Q. empty () Q. pop (); for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {vis [I] [J]. clear () ;}} cur. X = SX; cur. y = sy; cur. step = 0; cur. bomb = 0; cur. statew = STW; vis [SX] [sy]. push_back (cur); q. push (cur); While (! Q. empty () {now = Q. top (); q. pop (); Nx = now. x; ny = now. y; nstep = now. step; nbomb = now. bomb; nstw = now. statew; for (I = 0; I <4; I ++) {Tx = NX + dx [I]; ty = ny + dy [I]; t = (tx-1) * m + ty-1; If (TX <1 | TX> N | ty <1 | ty> m) continue; if (nbomb = 0 & (nstw & (1ll <t) & MP [TX] [ty] =-1) continue; // The Wall curstw = nstw; If (nstw & (1ll <t) & MP [TX] [ty]) curstw = nstw &~ (1ll <t); // obtain if (isok (TX, Ty) {cur. X = TX; cur. y = ty; If (Tx = ex & ty = ey) // determine the end point here. It's faster and start to judge TLE outside {ans = nstep + 1; return true;} cur. bomb = nbomb; cur. statew = curstw; cur. step = nstep + 1; if (nstw & (1ll <t) & MP [TX] [ty]) {If (MP [TX] [ty] =-1) cur. bomb --, cur. step ++; else cur. bomb + = MP [TX] [ty];} vis [TX] [ty]. push_back (cur); q. push (cur) ;}}return false;} int main () {int I, j, T; while (scanf ("% d", & N, & M), N | M) {memset (MP, 0, sizeof (MP); STW = 0; for (I = 1; I <= N; I ++) {scanf ("% s", S); For (j = 1; j <= m; j ++) {T = (I-1) * m + J-1; If (s [J-1] = 's') SX = I, Sy = J; else if (s [J-1] = 'D ') ex = I, ey = J; else if (s [J-1] = 'X') {MP [I] [J] =-1; STW = (1ll <t) | STW;} else if (s [J-1]> = '1' & S [J-1] <= '9 ') {MP [I] [J] = s [J-1]-'0'; STW = (1ll <t) | STW ;}} if (BFS ()) printf ("% d \ n", ANS); else printf ("-1 \ n");} return 0 ;}
 

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.