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

    啊 真无语 ~

    这题 一读完 就有感觉是 记忆化搜索 但就是一下子写不出来 =-=

    后来参考了别人的代码 发现还是有模糊的地方

    好好记住就是了 ~

          touch  me

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 const int size = 110;
     6 int sum;
     7 int n , m;
     8 int dir[4][2]={1,0,0,1,-1,0,0,-1};
     9 int arr[size][size];
    10 int dp[size][size];
    11 
    12 int dfs( int x , int y )
    13 {
    14     int mmax = 0;
    15     if( dp[x][y] )
    16         return dp[x][y];
    17     for( int i = 1 ; i<=m ; i++ )
    18     {
    19         for( int j = 0 ; j<4 ; j++ )
    20         {
    21             int xx = x + (dir[j][0])*i;
    22             int yy = y + (dir[j][1])*i;
    23             if( arr[xx][yy]>arr[x][y] && xx>=0 && xx<n && yy>=0 && yy<n )
    24             {
    25                 mmax = max( mmax , dfs(xx,yy) );
    26             }
    27         }
    28     }
    29     dp[x][y] = mmax + arr[x][y];
    30     if( sum<dp[x][y] )
    31         sum = dp[x][y]; 
    32     return dp[x][y];
    33 }
    34 
    35 int main()
    36 {
    37     cin.sync_with_stdio(false);
    38     int i , j;
    39     while( cin >> n >> m )
    40     {
    41         sum = 0;
    42         memset( dp , 0 , sizeof(dp) );
    43         if( n==-1 && m==-1 )
    44             break;
    45         for( i = 0 ; i<n ; i++ )
    46         {
    47             for( j = 0 ; j<n ; j++ )
    48             {
    49                 cin >> arr[i][j];
    50             }
    51         }
    52         dfs( 0 , 0 );
    53         cout << sum << endl;
    54     }
    55     return 0;
    56 }
    View Code

    递归的思想 真的很重要 从后往前 得到结果~

    just follow your heart
  • 相关阅读:
    selenium获取Cookie操作
    分布式锁-常用技术方案
    合成模式(Composite)-结构型
    享元模式-结构型
    桥接模式-结构型
    适配器模式-结构型
    建造者模式-创建型
    单例和原型模式-创建型
    外观(门面)模式-结构型
    JDK QUEUE队列
  • 原文地址:https://www.cnblogs.com/radical/p/3844919.html
Copyright © 2011-2022 走看看