Hdu1078 fatmouse and cheese (memory-based search)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1078

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 Input
3 11 2 510 11 612 12 7-1 -1
Sample output
37

Sourcezhejiang university training Contest 2001

Question:

There is a map with a positive integer value on it, and then it is required to start from (0, 0), each time only to a point with a greater value than the current point, in addition, you can go up to K Steps in four directions each time, and the maximum value can be obtained at the end.

Solution:

Because the question requires four directions to go, so think of the search, and require the optimal solution. Therefore, you can use memory-based search. The basic questions of dynamic planning are quite classic.


The Code is as follows: (93 ms)

Run ID Submit time Judge status Pro. ID EXE. Time EXE. Memory Code Len. Language Author
10907677 10:55:49 Accepted 1078 93 ms 392 K 910 B G ++ Tianzi 4747tym

# Include <cstdio> # include <cstring> int DP [147] [147], map [147] [147]; int N, K; int XX [4] = {0, 0, 1,-1}; int YY [4] = {1,-1, 0}; int DFS (int x, int y) {int ans = 0, max = 0; If (! DP [x] [Y]) {for (INT I = 1; I <= K; I ++) {for (Int J = 0; j <4; j ++) {int dx = x + XX [J] * I; int DY = Y + YY [J] * I; if (dx> = 0 & DX <n & dy> = 0 & dy <n & map [x] [Y] <map [dx] [dy]) {ans = DFS (dx, Dy); // The amount of cheese that can be obtained in each path. If (ANS> MAX) max = ans ;}} DP [x] [Y] = MAX + map [x] [Y]; // maximum amount of cheese that can be obtained from DP [x] [Y]} return DP [x] [Y];} int main () {While (~ Scanf ("% d", & N, & K) {If (n =-1 & K =-1) break; memset (DP, 0, sizeof (DP); memset (MAP, 0, sizeof (MAP); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {scanf ("% d", & map [I] [J]) ;}} int TT = DFS (0, 0); printf ("% d \ n", TT);} 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.