zoj1107FatMouse and Cheese 记忆化搜索
来源:互联网 发布:私募公募 知乎 编辑:程序博客网 时间:2024/05/29 18:29
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those 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 eat before being unable to move.
Input Specification
There are several test cases. Each test case consists of
- a line containing two 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,1), ... (1,n-1), and so on.
Output Specification
For each test case output in 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
Output for Sample Input
37
import java.io.BufferedInputStream;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Scanner;public class Main {public static void main(String[] args) {new Task().solve();}}class Task {Scanner in = new Scanner(new BufferedInputStream(System.in)) ;PrintWriter out = new PrintWriter(System.out);int[][] d = {{-1,0},{0,-1},{1,0},{0,1}} ;int n , k ; int[][] num ;int[][] dp ; boolean cango(int x , int y){return 0 <= x && x < n && 0 <= y && y < n ;}int dfs(int x , int y){if(dp[x][y] != -1){return dp[x][y] ;}int sum = 0 ;for(int i = 0 ; i < 4 ; i++){for(int step = 1 ; step <= k ; step++){int nx = d[i][0] * step + x ;int ny = d[i][1] * step + y ;if(cango(nx, ny) && num[nx][ny] > num[x][y]){sum = Math.max(sum, dfs(nx, ny)) ;}}}return dp[x][y] = sum + num[x][y] ;}void solve(){while(true){n = in.nextInt() ;k = in.nextInt() ;if(n == -1 && k == -1){break ;}num = new int[n][n] ;dp = new int[n][n] ;for(int i = 0 ; i < n ; i++){for(int j = 0 ; j < n ; j++){num[i][j] = in.nextInt() ;} Arrays.fill(dp[i] , -1) ;}out.println(dfs(0, 0)) ;}out.flush(); }}
- zoj1107FatMouse and Cheese (记忆化搜索)
- zoj1107FatMouse and Cheese 记忆化搜索
- hdu1078 FatMouse and Cheese(记忆化搜索)
- HDU1078 FatMouse and Cheese 【记忆化搜索】
- HDU1078_FatMouse and Cheese【记忆化搜索】
- hdoj1078 FatMouse and Cheese【记忆化搜索】
- hdu1078 FatMouse and Cheese 记忆化搜索
- HDU1078 FatMouse and Cheese记忆化搜索
- hdu1078FatMouse and Cheese【记忆化搜索】
- hdu1078_FatMouse and Cheese记忆化搜索
- hdoj1078 FatMouse and Cheese(记忆化搜索)
- FatMouse and Cheese(记忆化搜索)
- hdu1078 fatmouse and cheese 记忆化搜索
- hdu1078 FatMouse and Cheese【记忆化搜索】
- hdu 1078 fatmouse and cheese 记忆化搜索
- hdu 1078 FatMouse and Cheese(深搜----记忆化搜索)
- hdu 1078 FatMouse and Cheese(dp 记忆化搜索)
- hdu 1078 FatMouse and Cheese(记忆化搜索)
- 三分钟学会用SpringMVC搭建最小系统(超详细)
- RocketMQ原理解析-producer 4.发送分布式事物消息
- 查看mysql的版本
- Android流式布局
- Mac 使用virtualenv安装 matplotlib的解决方案
- zoj1107FatMouse and Cheese 记忆化搜索
- Android studio链接不上adb
- spring 学习笔记
- Oracle日期格式化操作
- 欢迎使用CSDN-markdown编辑器
- Java_基础—List集合存储学生对象并遍历
- iOS 多语言版本的开发(一)
- jquery serializeArray()、serialize()增加数据
- 决策树算法Matlab实现(train+test)