zoukankan      html  css  js  c++  java
  • hdu 1078 记忆化搜索

    我的第一道记忆化搜索题。这题是一个经典问题,网上搜解题报告大多是直接说用记忆化搜索,然后就贴代码了。我花了老大劲才弄明白记忆化搜索是咋回事。其实记忆化搜索也就是搜索,只不过用了DP的思想,开了状态数组以避免普通搜索过程中对状态的重复运算,从而大大提高了效率。

    /*
    * hdu1078/win.cpp
    * Created on: 2011-10-1
    * Author : ben
    */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;

    const int SIZE = 110;
    const int move[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
    int map[SIZE][SIZE], dp[SIZE][SIZE];
    int N, K;

    int dfs(int x0, int y0) {
    int x, y, ret = 0, temp;
    if (dp[x0][y0] < 0) {
    for (int i = 0; i < 4; i++) {
    for (int j = 1; j <= K; j++) {
    x = x0 + move[i][0] * j;
    y = y0 + move[i][1] * j;
    if (x >= 0 && y >= 0 && x < N && y < N) {
    if (map[x][y] > map[x0][y0]) {
    temp = dfs(x, y);
    if (temp > ret) {
    ret = temp;
    }
    }
    }
    }
    }
    dp[x0][y0] = ret + map[x0][y0];
    }
    return dp[x0][y0];
    }

    int main() {
    #ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
    #endif
    while (scanf("%d%d", &N, &K) == 2) {
    if (N < 0 || K < 0) {
    break;
    }
    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
    scanf("%d", &map[i][j]);
    }
    }
    memset(dp, -1, sizeof(dp));
    printf("%d\n", dfs(0, 0));
    }
    return 0;
    }



  • 相关阅读:
    React 生命周期
    React 总结
    系统后台设置
    数据库的事务日志已满,起因为"LOG_BACKUP"。
    webpack 打包器
    地图API
    ES6 与 React
    前端流行的技术
    Javascript 函数声明、函数表达式与匿名函数自执行表达式
    Javascript 解读与思想
  • 原文地址:https://www.cnblogs.com/moonbay/p/2197753.html
Copyright © 2011-2022 走看看