Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1078
Problem descriptionfatmouse have stored some cheese in a city. The city can being considered as a square grid of dimension N:each grid location are labelled (P,Q) where 0 <= p < n an D 0 <= Q < n. At each of the grid location Fatmouse have hid between 0 and blocks of cheese in a hole. Now he's going to enjoy him favorite food.
Fatmouse begins by standing on location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is a there are a super Cat named Top Killer sitting near his hole, so each time he can run at most K locatio NS to get into the hole before being caught by Top Killer. What's worse--after eating up the cheese at one location, Fatmouse gets fatter. So-in order-gain enough-he-has-to-run to-a location which had more blocks of cheese than thos E 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 ea T before being unable to move.
Inputthere is several test cases. Each test case consists of
A line containing 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,0), ... (1,n-1), and so on.
The input ends with a pair of-1 ' s.
Outputfor each test case output with a line the single integer giving the number of blocks of cheese collected.
Sample INPUT3 2 510 612 7-1-1 Sample Output37Main Topic to a square, a mouse from (0,0), each can only traverse or vertical walk up to K lattice, and arrive at the local weight is larger than the current lattice, to seek maximum weight andmemory Search After reading the topic, it is easy to think of Dfs, but without optimization, it will definitely time out, it is necessary to remember the search. A simple simulation of Dfs can be found that there are a lot of repeated searches, if we will repeat the search to save, can be much faster, so you can use DP[I][J] to indicate that (I,J) as the starting point for DFS results, so the next time you sweep (i,j) do not need to sweep again.
1#include <iostream>2#include <cstring>3#include <cmath>4#include <string>5#include <algorithm>6 using namespacestd;7 8 intmap[ the][ the];9 intdp[ the][ the];Ten intN, K; One intd[2][4] ={ -1,1,0,0,0,0, -1,1 }; A - intDfsintXinty) { - intAns =0; the if( !Dp[x][y]) { - for(inti =1; I <= K; i++ ) - for(intj =0; J <4; J + + ){ - intDX = x + d[0][J] *i; + intDy = y + d[1][J] *i; - if(DX <0|| DX >= N | | Dy <0|| DY >= N)Continue; + if(Map[dx][dy] >Map[x][y]) { AAns =max (ans, dfs (dx, dy)); at } - } -Dp[x][y] = ans +Map[x][y]; - } - returnDp[x][y]; - } in - intMain () { toIos::sync_with_stdio (false ); + - while(Cin >> n >> K, (n +1) || (k +1 ) ){ the for(inti =0; I < n; i++ ) * for(intj =0; J < N; J + + ) $CIN >>Map[i][j];Panax NotoginsengMemset (DP,0,sizeof(DP)); -cout << DFS (0,0) <<Endl; the } + A return 0; the}
HDU-1078 Fatmouse and Cheese (memory search)