Chapter06-FatMouse and Cheese(ZOJ 1107)(搜索记忆DP)

FatMouse and Cheese


Time Limit: 10 Seconds      Memory Limit: 32768 KB


FatMouse has stored some cheese in a city. The citycan be considered as a square grid of dimension n: each grid location islabelled (p,q) where 0 <= p < n and 0 <= q < n. At each gridlocation Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Nowhe's going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eatsup the cheese where he stands and then runs either horizontally or verticallyto another location. The problem is that there is a super Cat named Top Killersitting near his hole, so each time he can run at most k locations to get intothe hole before being caught by Top Killer. What is worse -- after eating upthe cheese at one location, FatMouse gets fatter. So in order to gain enoughenergy for his next run, he has to run to a location which have more blocks ofcheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at eachgrid location, compute the maximum amount of cheese FatMouse can eat beforebeing unable to move.

Input Specification

There are several test cases. Each test case consistsof

  • 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.

The input ends with a pair of -1's.

Output Specification

For each test case output in a line the single integergiving the number of blocks of cheese collected.

Sample Input

3 1

1 2 5

10 11 6

12 12 7

-1 -1

Output for Sample Input

37

 

 

【题目大意】:在一个n*n的方格中,每个方格存放的奶酪数为0~100;老鼠从坐标0,0出发,每次只能垂直行走或水平行走,且最远能走的距离为k,要求走过的格子奶酪的个数递增,现求出能获得的最大的奶酪数量;

【分析】这道题,必然是BFS或DFS搜索,但是显然消耗的时间是指数的,可不可以优化呢?能否公共子问题呢?用常用的记忆搜索DP的方法,但是不能从0,0位置开始搜索,我们必须从奶酪最多的方格开始搜索,依次递减;

于是有:假如奶酪递减的位置为a,b,c,d,e。。。我们先找位置a,显然此时的max[a]= count[a];再从位置b开始,显然位置b看能否直接到达a,如果可以,那么max[b] = max [a]+ count[b],否则max[b] = count[b];依次类推,到了e点,我们需要判断他是否能够到达a,b,c,d,e点的每一个,如果可以到达a和c,那么我们取max[e]=Math.max(max [a]+ count[e]; max[c]+ count[e])即可;

备注:注意还需要判断一下,因为可能存在奶酪数可能是一样的情况;

是不是发现很简单呢?开始写代码吧

Java代码如下:

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	private int k;
	private int n;
	private LinkedList<Node> lilist;
	private int max[][];
	private int count[][];
	public Main(int n,int k){
		
		this.n = n;
		this.k = k;
		
		lilist =  new LinkedList<Node>();
		max = new int[n][n];
		count = new int[n][n];
		for(int i=0;i<n;i++){
			Arrays.fill(max[i], 0);
			Arrays.fill(count[i], 0);
		}
	
		
	}
	
	public void addElment(Node n){
		lilist.add(n);
		count[n.x][n.y] = n.count;
	}
	
	public static void main(String[] args) {
		
		Scanner cin = new Scanner(System.in);
		int n,k;
		n = cin.nextInt();
		k = cin.nextInt();
		Main ma;
		
		while(!(n == -1 && k == -1)){
			
			ma = new Main(n,k);
			
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					ma.addElment(new Node(i,j,cin.nextInt()));
					
				}
			}
			System.out.println(ma.getMaxValue());
			n = cin.nextInt();
			k = cin.nextInt();
			
		}
	}

	private int getMaxValue() {
		// TODO Auto-generated method stub
		Collections.sort(lilist);
		Node vert;
		int maxtemp,xtemp,ytemp;
		
		while(lilist.size() >0 ){
			vert = lilist.remove();
			xtemp = vert.x;
			ytemp = vert.y;
			maxtemp = 0;
			//往4个方便走到尽头;
			for(int i=1;i<=k;i++){
				//计算往x方向;
				if(xtemp+i < n){
					if(max[xtemp+i][ytemp] != 0){
						if(count[xtemp+i][ytemp] != count[xtemp][ytemp]){
							//在这种情况下,才能算出;
							maxtemp = Math.max(maxtemp, max[xtemp+i][ytemp]);
						}
					}
				}
				if(xtemp-i >= 0){
					if(max[xtemp-i][ytemp] != 0){
						if(count[xtemp-i][ytemp] != count[xtemp][ytemp]){
							//在这种情况下,才能算出;
							maxtemp = Math.max(maxtemp, max[xtemp-i][ytemp]);
						}
					}
				}
				//y方向;
				if(ytemp+i < n){
					if(max[xtemp][ytemp+i] != 0){
						if(count[xtemp][ytemp+i] != count[xtemp][ytemp]){
							//在这种情况下,才能算出;
							maxtemp = Math.max(maxtemp, max[xtemp][ytemp+i]);
						}
					}
				}
				if(ytemp-i >= 0){
					if(max[xtemp][ytemp-i] != 0){
						if(count[xtemp][ytemp-i] != count[xtemp][ytemp]){
							//在这种情况下,才能算出;
							maxtemp = Math.max(maxtemp, max[xtemp][ytemp-i]);
						}
					}
				}
			}
			
			max[xtemp][ytemp] = maxtemp + count[xtemp][ytemp];
			
			if(vert.isBothZero()){
				return max[0][0];
			}
			
		}
		
		return 0;
	}

	static class Node implements Comparable<Node>{
		
		int x, y, count;
		
		public Node(int x,int y,int count) {
			this.x = x;
			this.y = y;
			this.count = count;
		}

		@Override
		public int compareTo(Node o) {
			// TODO Auto-generated method stub
			
			if(count > o.count){
				return -1;
			}else if(count < o.count){
				return 1;
			}
			
			return 0;
		}
		public boolean isBothZero(){
			if(x==0 && y==0){
				return true;
			}
			return false;
		}

	
		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值