现在的位置: 首页 > 综合 > 正文

zoj1107记忆化搜索

2014年10月24日 ⁄ 综合 ⁄ 共 2591字 ⁄ 字号 评论关闭
文章目录
FatMouse and Cheese


Time Limit: 10 Seconds      Memory Limit: 32768 KB


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.

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

Output Specification

For each test case output in a line the single integer giving 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

代码:
#include <iostream>
#include<string.h>

using namespace std;
int n;//网格的大小
int k;//每次最多移动的步数
int grid[105][105];//保存奶酪数量的网络矩阵
int mem[105][105];//用于记忆式搜索保存中间的结果
int maxnum =0;//FatMouse吃掉的奶酪
//实现记忆式搜索,形参是当前的搜索位置
int memSearch(int r,int c)
{
    int r1,c1;//fatMouse将要到达的下一个位置
    int i;
    //如果不是-1,表示该网格已被搜索过,直接读取结果
    if(mem[r][c]!= -1)
        return mem[r][c];
    //每次让FatMouse前进1步,直到k步
    for(i=1; i<=k; i++)
    {
        //向上前进1步
        r1 = r-i;
        //如果下一步是有效的,且下一步奶酪的数量比当前多
        if(r1>=1&&r1<=n&&grid[r][c]<grid[r1][c1])
        {
            mem[r1][c1]=memSearch(r1,c1);//移动下一步继续搜索
            //移动下一步后奶酪的总数量是增长的
            if(mem[r1][c1]>maxnum)
                maxnum = mem[r1][c1];
        }

        //向下前进一步
        r1 = r+1;
        if(r1>=1&&r1<=n&&grid[r][c]<grid[r1][c1])
        {
            mem[r1][c1] = memSearch(r1,c1);//移动到下一步继续搜索
            if(mem[r1][c1]>maxnum)
                maxnum = mem[r1][c1];
        }
        //向左前进一步
        c1 = c-1;
        if(r1>=1&&r1<=n&&grid[r][c]<grid[r1][c1])
        {
            mem[r1][c1] = memSearch(r1,c1);//移动到下一步继续搜索
            if(mem[r1][c1]>maxnum)
                maxnum = mem[r1][c1];
        }
        //
        c1 = c+1;
        if(r1>=1&&r1<=n&&grid[r][c]<grid[r1][c1])
        {
            mem[r1][c1] = memSearch(r1,c1);//移动到下一步继续搜索
            if(mem[r1][c1]>maxnum)
                maxnum = mem[r1][c1];
        }
    }
    return maxnum + grid[r][c];//吃掉当前格子中的奶酪
}

int main()
{
    int i,j;
    while(cin>>n>>k)
    {
        if(n==-1&&k==-1)
            break;
        //读取数据,建立grid矩阵
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                cin>>grid[i][j];
            }
        }
        //用于搜索的mem数组初始化,-1表示该网格没有搜索过
        memset(mem,-1,sizeof(mem));


        cout<<memSearch(1,1)<<endl;
    }

    return 0;
}

代码注释已经很详细了,相信能轻易看懂。

抱歉!评论已关闭.