Fatmouse and CheeseTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5463 Accepted Submission (s): 2227
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
Sourcezhejiang University Training Contest 2001
Recommendwe carefully selected several similar problems for you:1080 1074 1024 1081 1025
Test instructions: Give N*n number Matrix, starting from (0,0), go to the next lattice on the number of more than the number on the current lattice, and each time you can move along a straight line up to K position, that is, each time there is a 4*k choice, ask the sum of all the last number of the maximum value.
Idea: Memory search.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 105#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n" ) typedef long Long ll;using namespace Std;int dir[4][2]={0,1,1,0,0,-1,-1,0};int mp[maxn][maxn];int sum[maxn][maxn];int N,k;bool Isok (int x,int y) {if (x>=0&&x<N&&y>=0&&y<n) return true; return false;} int dfs (int x,int y) {if (Sum[x][y]) return sum[x][y]; direct return int maxx=0; int i,j; FRE (i,1,k) {FRL (j,0,4) {int dx=x+dir[j][0]*i; int dy=y+dir[j][1]*i; if (Isok (Dx,dy) &&mp[x][y]<mp[dx][dy]) {Maxx=max (Maxx,dfs (Dx,dy)); }}} Sum[x][y]=maxx+mp[x][y]; Record the best answer from (x, y), and return to this value (x, y) The next time you come back to Sum[x][y];} int main () {int i,j; while (SFF (n,k)) {if (n==-1&&k==-1) break; FRL (i,0,n) FRL (j,0,n) SF (mp[i][j]); MEM (sum,0); PF ("%d\n", DFS (0,0)); } return 0;}
Fatmouse and Cheese (HDU 1078 memory Search)