HDU-3502-Huson's Adventure Island (BFS + pressure DP)

Source: Internet
Author: User
Problem descriptiona few days ago, Tom was tired of all the PC-games, so he went back to some old FC-games. "Hudson's Adventure Island" was his favorite which he had played thousands of times. but to his disappointed, the more he played, the more he lost. tom soon gave up the game.



Now, he has ts an easier game on the base of "Hudson's Adventure Island". He wants to know whether the new game is easy enough. So he came to you for help.
To simplify the problem, you can assume there is matrix-map contains M (0 <m <256) rows, n (0 <n <256) columns, an entrance on the top-left corner (0, 0), an exit on the bottom-right corner (m-1, n-1 ). each entry of the matrix contains a integer k. the range of K is defined below:
A) k = 0, it is a free space one can go through.
B) k =-1, it is an obstacle one can't go through.
C) 0 <k <10000, It is a fruit one can go through and gain K points of energy.
D) k> = 0 at the entrance point and the end point.
At the begin, the hero has 0 Points of energy and he can go four directions ctions (Up, down, left, right ). each move he makes cost 1 point of energy. no energy no move. if he can't make a move or get to the exit, he loses the game. the number of fruit is 17 at most.
 
Inputthe input consists of multiple test cases. Each test case starts with two positive integers m, n. Then follows M lines, each line contain N integers.
Outputfor each case, if the hero can get the exit, find and print the maximum points of energy he left. or print "You loss !"
Sample Input
4 48 0 0 0-1 -1 -1 0-1 -1 -1 00 6 0 03 34 0 00 0 00 0 04 45 0 0 00 -1 -1 00 -1 -1 00 0 0 0
 
Sample output
40you loss!HintThe hero can pass the exit. When he gets the exit point, he can choose to get out to finish the game or move on to gain more points of energy. 
 
Authorimems
Source2010 ACM-ICPC multi-university training Contest (7) -- host by hit


Idea: first use BFs to find the distance between each fruit and the end point, and then use status compression DP to solve the problem.


# Include <cstdio> # include <queue> # define INF 999999999 # define max (A, B) (A> B? A: B) using namespace STD; struct s {int X, Y;} Fruit [20], T; int n, m, CNT, ANS, MP [256] [256], DIS [20] [20], cost [256] [256], NXT [4] [2] = }, {}, {-}, {0,-1 }}, MX [1 <18] [20], Val [20]; void BFS (s t) {int I, j, NX, NY; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) cost [I] [J] = inf; queue <S> que; cost [T. x] [T. y] = 0; que. push (t); While (! Que. empty () {T = que. front (); for (I = 0; I <4; I ++) {Nx = T. X + NXT [I] [0]; ny = T. Y + NXT [I] [1]; if (nx> = 0 & NX <n & ny> = 0 & ny <M & MP [NX] [NY]! =-1 & cost [NX] [NY] = inf) {cost [NX] [NY] = cost [T. x] [T. y] + 1; T. X = NX; T. y = NY; que. push (t);} t = que. front ();} que. pop () ;}} int main () {int I, j; while (~ Scanf ("% d", & N, & M) {CNT = 0; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {scanf ("% d", & MP [I] [J]); if (MP [I] [J]> 0) {fruit [CNT]. X = I; fruit [CNT]. y = J; Val [CNT ++] = MP [I] [J] ;}} if (n = 1 & M = 1) {If (MP [0] [0]> = 0) printf ("% d \ n", MP [0] [0]); else printf ("You loss! \ N "); continue;} If (MP [0] [0] <= 0) {printf (" You loss! \ N "); continue;} If (MP [n-1] m-1] =-1) {printf (" You loss! \ N "); continue;} Fruit [CNT]. X = n-1; // The end point is also included in fruit [CNT]. y = m-1; for (I = 0; I <= CNT; I ++) {BFS (fruit [I]); For (j = 0; j <= CNT; j ++) dis [I] [J] = cost [fruit [J]. x] [fruit [J]. y]; // the distance between each fruit and its destination} queue <S> que; for (I = 1; I <(1 <CNT); I ++) for (j = 0; j <CNT; j ++) MX [I] [J] =-1; int V; T. X = 1; T. y = 0; MX [T. x] [T. y] = Val [0]; que. push (t); While (! Que. Empty () {T = que. Front (); for (I = 1; I <CNT; I ++) {If (! (T. X & (1 <I) {v = Mx [T. x] [T. y]-Dis [T. y] [I]; If (V> = 0) {T. X = (T. X | (1 <I); T. y = I; If (V + val [I]> MX [T. x] [I]) {MX [T. x] [I] = V + val [I]; que. push (t);} t = que. front () ;}} que. pop () ;}ans =-1; for (I = 1; I <(1 <CNT); I ++) for (j = 0; j <CNT; j ++) ans = max (ANS, MX [I] [J]-Dis [J] [CNT]); If (ANS> = 0) printf ("% d \ n", ANS); else puts ("You loss! ");}}

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.