Fatmouse and cheese
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2859 accepted submission (s): 1118
Problem descriptionfatmouse has stored some cheese in a city. the city can be considered as a square grid of dimension n: each grid location is labeled (p, q) where 0 <= P <n and 0 <= q <n. at each grid location fatmouse has hid between 0 and 100 blocks of cheese in a hole. now he's going to enjoy his favorite food.
Fatmouse begins by standing at location (0, 0 ). he eats up the cheese where he stands and then runs either horizontally or vertically to another location. the problem is that there is a super cat named top killer sitting near his hole, so each time he can run at most K locations to get into the hole before being caught by top killer. what is worse -- after eating up the cheese at one location, fatmouse gets fatter. so in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given N, K, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese fatmouse can eat before being unable to move.
Inputthere are several test cases. Each test case consists
A line containing two integers between 1 and 100: N and K
N lines, each with N numbers: the first line contains the number of blocks of cheese at locations (0, 0) (0, 1 )... (0, n-1); the next line contains the number of blocks of cheese at locations ),... (1, n-1), and so on.
The input ends with a pair of-1's.
Outputfor each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample input3 1 1 2 5 10 11 6 12 12 7-1-1
Sample output37
Sourcezhejiang university training Contest 2001 DP. To ensure the optimum. In the ascending order of DP. Or you can use the memory-based search function to better understand it.
# Include <stdio. h> # Include <Algorithm> # Include < String . H> # Include <Iostream> Using Namespace STD; Const Int Maxn = 110 ; Int A [maxn] [maxn]; Int DP [maxn] [maxn]; Struct Node { Int X, Y; Int Val;} node [maxn * Maxn]; Bool CMP (node A, Node B ){ Return A. Val < B. Val ;} Int Main (){ Int N, K; While (Scanf ( " % D " , & N, & K) = 2 ){ If (N =- 1 & Amp; k =- 1 ) Break ; Int CNT = 0 ; For ( Int I = 0 ; I <n; I ++) For ( Int J = 0 ; J <n; j ++ ) {Scanf ( " % D " ,& A [I] [J]); If (I! = 0 | J! = 0 ) {Node [CNT]. x =I; node [CNT]. Y = J; node [CNT ++]. Val = A [I] [J] ;}} sort (node, node + CNT, CMP); memset (DP, - 1 , Sizeof (DP); DP [ 0 ] [ 0 ] = [ 0 ] [ 0 ]; Int Ans = DP [ 0 ] [ 0 ]; For ( Int I = 0 ; I <CNT; I ++ ){ Int X = Node [I]. X; Int Y = Node [I]. Y; For ( Int Xx = max ( 0 , X-k); XX <= min (n- 1 , X + k); XX ++ ){ If (A [XX] [Y]> = A [x] [Y]) Continue ; If (DP [XX] [Y] =- 1 ) Continue ; DP [x] [Y] = Max (DP [x] [Y], DP [XX] [Y] + A [x] [Y]);} For ( Int YY = max (0 , Y-k); YY <= min (n- 1 , Y + k); YY ++ ){ If (A [x] [YY]> = A [x] [Y]) Continue ; If (DP [x] [YY] =- 1 ) Continue ; DP [x] [Y] = Max (DP [x] [Y], DP [x] [YY] + A [x] [Y]);} ans = Max (ANS, DP [x] [Y]);} printf ( " % D \ n " , ANS );} Return 0 ;}