The first time to do the problem of memory search to find the information on the Internet reference to the online interpretation of the memory search
1. The idea of memory search
the idea of memory search is , There will be a lot of repeated calculations during the search process. , If we can record the answers to some states, we can reduce the amount of duplicate searches.
2. Application scope of memory search
According to the thought of the memory search, it is to solve Repeat calculation , but not Duplicate Build , that is to say, these searches must be the subject of a step calculation in the process of searching the extension path, that is, the topic of " search answer and path related " , but not the subject of searching for a path before it can be calculated. It has to be calculated in step, and in the search process, a search result must be built on the results of the same type of problem, which is similar to a dynamic planning solution.
That is to say, his problem is not simply to create a walk plan, but to generate a walk plan cost, and every step, in the search tree / graph to generate a new state, can be accurately calculate the cost of the end, that is, can be calculated in step, So you can apply the answers you've got.
3, the core realization of memory search
A. First, a table is used to record the search results that have been stored, which is generally implemented with a hashtable
B. state representation, because it is to be implemented with a hash table, so the state is best can be expressed in numbers, A common method is to write a state into a p - binary number, and then use the decimal number corresponding to the number as the state
c. at the beginning of each state search, efficient use of hash table to search whether this state has occurred, if already done, call the answer directly, backtracking // This is the Marker optimization acceleration
D. if not, search by normal method
4, the memory search is similar to the dynamic programming, the difference is that it is inverted " recursive dynamic planning ."
Fatmouse and CheeseTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8104 Accepted Submission (s): 3388
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
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> #define MAX 105# Define MAX (A, B) a>b?a:busing namespace Std;int map[max][max],vis[max][max];int step[4][2]={0,1,0,-1,1,0,-1,0};int Max_zi,n,k,nx,ny;int DPS (int x,int y) {int maxx=0;//each must have an initial maximum if (vis[x][y]!=0) return vis[x][y];//If it is already computed, no more calculations. for (int i=0;i<4;i++)//four direction traverse {for (int j=1;j<=k;j++)//per direction Walk K units {nx=x+step[i][0]*j; Ny=y+step[i][1]*j; if (nx>=0&&ny>=0&&nx<n&&ny<n)//Cross range {if (map[nx][ny]>map[x ][y])//The new number must be greater than the previous number in accordance with test instructions {Max_zi=dps (nx,ny);//Find out the maximum value of the next Dfs Maxx=max ( Maxx,max_zi);//Select the maximum value of the traverse in this point}}} return vis[x][y]=map[x][y]+maxx;//the maximum number of points in each direction + The number of the current point}int main (void) {while (~scanf ("%d%d", &n,&k)) {memset (vis,0,sizeof (VIS)); if (n==-1| | k==-1) {return 0; } for (int i=0;i<n;i++) {for (int ii=0;ii<n;ii++) {scanf ("%d", &am P;MAP[I][II]); }} printf ("%d\n", DPS (0,0)); } return 0;}
HDU 1078 Fatmouse and Cheese "Memory Search"