P-fatmouse and Cheese HDU 1078 (Memory search)

Source: Internet
Author: User


P-fatmouse and Cheese
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status Practice HDU 1078

Description
Fatmouse 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.


Input
There 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.


Output
The For all test case is output in a line, the single integer giving the number of blocks of cheese collected.


Sample Input

3 1
1 2 5
10 11 6
12 12 7
-1-1



Sample Output

37

#include <cstdio> #include <algorithm> #include <cstring>using namespace Std;const int Maxn=222;const int inf=999999999;int a[maxn][maxn];int vis[maxn][maxn];int n,k;int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};int dfs (int x, int y) {//the largest and    if (Vis[x][y]) return from the current point to vis[x][y];    int ans=0;    for (int j=1;j<=k;j++) for    (int i=0;i<4;i++) {        int xx=j*dx[i]+x;        int yy=j*dy[i]+y;        if (Xx>=0&&xx<n&&yy>=0&&yy<n&&a[x][y]<a[xx][yy]) {           Ans=max ( Ans,dfs (Xx,yy));        }    }    Vis[x][y]=ans+a[x][y];    return vis[x][y];} int main () {    while (scanf ("%d%d", &n,&k) && (n!=-1&&k!=-1)) {for        (int i=0;i<n;i++ {            for (int j=0;j<n;j++) {                scanf ("%d", &a[i][j]);}        }        memset (vis,0,sizeof (Vis));        printf ("%d\n", DFS (0,0));    }    return 0;}



P-fatmouse and Cheese HDU 1078 (Memory search)

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.