Hdu--1078--fatmouse and cheese--Memory search

Source: Internet
Author: User

Fatmouse and CheeseTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 5128 Accepted Submission (s): 2078

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 Input
3 11 2 510 11 612 12 7-1-1

Sample Output
37

Test instructions: The mouse initially in the matrix of the NxN (0.0), each time a maximum of One direction to move the K lattice, each move past the lattice must be larger than the current lattice inside the value of the sum of all the values of the path to find the maximum value

Analytical:

The memory search, according to test instructions, had to diffuse the search, which is the kind of brute force search at a point where it can go, and that will time out.

Memory search is implemented as follows: can be understood as the result of the formation is reversed, from the end point to the beginning (0,0) slowly formed, each search a point around can meet the conditions of the point, each find a point to continue to find the point around the points to meet conditions, the final point will be found to be the largest, that is, the end point In this case, the values in the matrix map are recorded as the maximum value of the reverse search for all paths that use it as the end point, and then the upward layer returns this value, and for this point in the previous layer, the value of the distance below is the maximum value of the number, so the values returned and this point are added together as the maximum value.

I know that the light said is not how to understand, so drew a picture, convenient for everyone to observe, but also to review the time to look, calculate to make a note.



WA once, the reason is to write code when listening to the song, did not consider K

Tle one time because of the direct proliferation violence search


#include <iostream> #include <cstdio> #include <cstring> #define MM (A, b) a<b?b:ausing namespaceStd;intN,M,Map[111][111],Sum[111][111],Dd[4][2]={0,1,0,-1,1,0,-1,0};intDfs(intX,intY){intI,J,K,L=0,X,Y;if(Sum[X][Y])returnSum[X][Y]; for(I=0;I<4;I+ +)//traversal of four directions { for(J=1;J<=M;J+ +)//traverse each distance 1-k {X=X+Dd[I][0]*J;//Pick pointsY=Y+Dd[I][1]*J;if(X>=0&&Y>=0&&X<N&&Y<N)//judgment out of bounds {if(Map[X][Y]>Map[X][Y]//The next point must be larger than the value of the current point {K=Dfs(X,Y);//k records the maximum value of the next node (x, y)L=Mm(L,K);//Select the largest} }}}Sum[X][Y]=L+Map[X][Y];//can go to the maximum of each point and the value of the current point = the maximum value of the current pointreturnSum[X][Y];//returns the maximum value of the current point}intMain (void){intI,J,K,L; while(scanf("%d%d",&N,&M)!=Eof)    {if(N==-1&&M==-1) Break; for(I=0;I<N;I++) { for(J=0;J<N;J++) {scanf("%d",&Map[I][J]); }        }Memset(Sum,0,sizeof(Sum));L=Dfs(0,0);Printf("%d\n",L); }return0;}

Sensory Memory Search is a time-consuming way to store certain states in the search process to remove duplicate searches


Hdu--1078--fatmouse and cheese--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.