hdu1078 Memory Dfs

Source: Internet
Author: User

Fatmouse and Cheese

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 1 1 2 5 10 11 6 12 12 7-1-1 Test instructions: The mouse can only go to the LIS hole, each time in Col or row direction to walk K step, to the maximum can eat to how big Cheese idea: Very simple DP, deep search attention memory Dfs can understand wrong, thought K step inside, every small step can col or row, In fact, each k has identified a single direction, so tle, but to cry, obviously yesterday did a similar memory, how will tle:cold_sweat:

#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<string>#include<vector>#include<algorithm>Const intINF =0x3f3f3f;Const intMAXN = 1e3+Ten;using namespacestd;intA[MAXN][MAXN];intDP[MAXN][MAXN];/*int mov[4][2] = {{1,1},{1,-1},{-1,1},{-1,-1}};*/intn,k;voidinit () {memset (DP,-1,sizeof(DP));}intCheckintXinty) {    if(x<0|| x>=n| | y<0|| Y>=n)return 0; Else return 1;}intDfsintXinty) {    if(dp[x][y]!=-1)returnDp[x][y]; Dp[x][y]=A[x][y]; intNx,ny; /*for (int i=k;i>=0;i--) {for (int j=k-i;j>=0;j--) {if (i+j==0) continue;                for (int w=0;w<4;w++) {nx = X+mov[w][0]*i;                NY = y+mov[w][1]*j;                    if (check (Nx,ny) &&a[x][y]<a[nx][ny]) {//cout<< "OK" <<endl;                    if (Dfs (Nx,ny) +a[x][y]>dp[x][y]) {dp[x][y] = Dp[nx][ny]+a[x][y]; }                }            }        }    }*/    //every time it's one-way.     for(intI=0;i<4; i++){       //NX = x+move[j][0]; //NY = y+move[j][1];NX =x; NY=y;  for(intj=1; j<=k;j++){            if(i==0) NX = x+J; Else if(i==1) NX = XJ; Else if(i==2) NY = y+J; ElseNY = yJ; if(Check (Nx,ny) &&a[x][y]<A[nx][ny]) {                //DFS (nx,ny,t+1);                if(Dfs (Nx,ny) +a[x][y]>Dp[x][y]) {Dp[x][y]= dp[nx][ny]+A[x][y]; }            }       }    }    returndp[x][y];}intMain () { while(SCANF ("%d%d", &n,&k)!=eof&&n!=-1) {init ();  for(intI=0; i<n;i++){             for(intj=0; j<n;j++) {scanf ("%d",&A[i][j]); }} DFS (0,0); /*for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {cout<<dp[i][j]<< "";        } cout<<endl; }*/cout<<dp[0][0]<<Endl; }    //cout << "Hello world!" << Endl;    return 0;}
View Code

hdu1078 Memory 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.