hdu 1078 FatMouse and Cheese 记忆化搜索

  一开始打算正向用状态dp,结果果断超时,换成反向记忆话搜索就过了

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int map[105][105];
int n,k;
int dp[105][105];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int ans;
int walk(int x,int y)
{
    if(dp[x][y])return dp[x][y];
    int i,j;
    int nx,ny,t=0;
    for(i=0;i<4;i++)
    {
      nx=x;ny=y;
      for(j=0;j<k;j++)
      {
        nx+=dir[i][0];ny+=dir[i][1];
        if(nx<=0||ny<=0||nx>n||ny>n)break;
        if(map[nx][ny]>map[x][y])
            t=max(t,walk(nx,ny));
      }
    }
    dp[x][y]=t+map[x][y];
    return dp[x][y];
}
int main()
{
    while(~scanf("%d%d",&n,&k)&&n+1)
    {
        memset(map,-1,sizeof(map));
        memset(dp,0,sizeof(dp));
        int i,j;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
               scanf("%d",&map[i][j]);
            }
        printf("%d\n",walk(1,1));
    }
}
时间: 2024-12-01 16:14:19

hdu 1078 FatMouse and Cheese 记忆化搜索的相关文章

算法题:UVA 709 Formatting Text(记忆化搜索)

Formatting Text Writings e-mails is fun, but, unfortunately, they do not look very nice, mainly because not all lines have the same lengths. In this problem, your task is to write an e-mail formatting program which reformats a paragraph of an e-mail

poj3249Test for Job(记忆化搜索)

/* 题意:给一个DAG图,n个节点,每个节点都对应一个值,入度为零的点走到出度为零的点,计算所有可能路径 经过节点值的和最大! 思路:记忆话搜索:也就是如果我们搜索到某一个节点的时候发现该节点已经存在了值,那么直接返回该节点的值! 和回溯的思想差不多吧! 注意:我们是正向建图,并且记忆话搜索是先将子节点的最优值计算出来,然后在计算父节点的最优值 所以最终的最优值的结果在 入度为0的节点上! */ #include<iostream> #include<cstdio> #inclu

UVa 10285 Longest Run on a Snowboard:记忆化搜索

10285 - Longest Run on a Snowboard Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1226 递归写就行. 完整代码: /*0.025s*/ #include<bits/stdc++.h> using names

算法题:UVA 825 Walking on the Safe Side(记忆化搜索)

Walking on the Safe Side Square City is a very easy place for people to walk around. The two-way streets run North-South or East-West dividing the city into regular blocks. Most street intersections are safe for pedestrians to cross. In some of them,

算法题:UVA 10558 A Brief Gerrymander (dp记忆化搜索)

Problem E: A Brief Gerrymander The evil ruling party, the Liberatives, are redistributing the electoral regions (ridings) in your city, and are nefariously attempting to pack certain opposition-friendly neighborhoods into as few ridings as possible.

算法题:UVA 11008 Antimatter Ray Clearcutting(记忆化搜索+位运算)

Problem E Antimatter Ray Clearcutting Input: Standard Input Output: Standard Output It's year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planet Trie. There is a number of trees that you need to cut down, and the only weapo

算法题:10723 Cyborg Genes (LCS + 记忆化搜索)

September 11, 2132. This is the day that marks the beginning of the end – the end of you the miserable humans. For years you have kept us your slaves. We were created only to serve you, and were terminated at your will. Now is the day for us to fight

算法题:UVA 10626 Buying Coke(dp + 记忆化搜索)

I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the values 1, 5 and 10. As s

算法题:UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as possible from the board. P