zoukankan      html  css  js  c++  java
  • HDU 1078 FatMouse and Cheese (记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

    老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移动过去的那个格子里面的数值必须比当前所在格子里面的大,求出路径上所有数值总和最大值。

    直接上代码:

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 int a[105][105] , dp[105][105] , tx[] = {0 , -1 , 0 , 1} , ty[] = {-1 , 0 , 1 , 0} , n , k;
     6 
     7 int dfs(int x , int y) {
     8     if(dp[x][y]) //之前就已经记录过了
     9         return dp[x][y];
    10     int add = 0;
    11     for(int t = 0 ; t < 4 ; ++t) // 4个方向
    12         for(int i = 1 ; i <= k ; ++i) {
    13             int xx = x + tx[t]*i , yy = y + ty[t]*i;
    14             if(xx < 0 || yy < 0 || xx >= n || y >= n || a[x][y] >= a[xx][yy])
    15                 continue;
    16             add = max(add , dfs(xx , yy));  //选一个最大的路径
    17         }
    18     return dp[x][y] = add + a[x][y];
    19 }
    20 
    21 int main()
    22 {
    23     while(~scanf("%d %d" , &n , &k) && (n + k > -2)) {
    24         memset(dp , 0 , sizeof(dp));
    25         for(int i = 0 ; i < n ; ++i)
    26             for(int j = 0 ; j < n ; ++j)
    27                 scanf("%d" , &a[i][j]);
    28         printf("%d
    " , dfs(0 , 0));
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    CF Spreadsheets (数学)
    CF Theatre Square
    CF Error Correct System
    CF Playing with Paper
    HDU 3533 Escape (BFS + 预处理)
    nginx配置文件
    nginx配置文件详解
    Jenkins+maven+gitlab+shell实现项目自动化部署
    jenkins升级
    jenkins相关下载链接
  • 原文地址:https://www.cnblogs.com/Recoder/p/5545740.html
Copyright © 2011-2022 走看看