[HDU 1078] FatMouse and Cheese (memory-based DFS)

Source: Internet
Author: User

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)

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.