Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1078
A fat mouse needs to eat cheese in a n * n-sized board. The mouse can go at most k units at a step, you must go to a point that is more than the current number of cheeses. Tell you the number of cheeses on each vertex in the board, and find the maximum number of cheeses that the mouse can eat.
Idea: similar to the memory DFS of the board DP, you can simply search and add the memory answer.
# Include <iostream> # include <stdio. h> # include <string. h >#include <algorithm> # define MAXN 120 using namespace std; int map [MAXN] [MAXN]; int f [MAXN] [MAXN]; int xx [] = {1,-, 0}, yy [] = {, 1,-1}; int n, k; int max (int, int B) {if (a> B) return a; return B;} bool inMap (int x, int y) {if (x <1 | x> n | y <1 | y> n) return false; return true;} int dfs (int x, int y) {if (f [x] [y]) return f [x] [y]; int ans = 0; for (int dist = 1; dist <= K; dist ++) for (int dir = 0; dir <4; dir ++) {int newx = x + xx [dir] * dist, newy = y + yy [dir] * dist; if (inMap (newx, newy) if (map [newx] [newy]> map [x] [y]) ans = max (ans, dfs (newx, newy);} ans + = map [x] [y]; return f [x] [y] = ans ;} int main () {while (scanf ("% d", & n, & k) & n! =-1 & k! =-1) {memset (f, 0, sizeof (f); for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) scanf ("% d", & map [I] [j]); printf ("% d \ n ", dfs (1, 1);} return 0 ;}
[HDU 1078] FatMouse and Cheese (memory-based DFS)