zoukankan      html  css  js  c++  java
  • UVa 10285 Longest Run on a Snowboard [DP]

    题意:给定一个整数矩阵,找到一条严格递减的最长路。

    简单的动规题目,dp[i][j]表示走到(i, j)所走过的最长步数,向四个方向转移状态即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn = 105;
     5 string str; int r, c;
     6 int a[maxn][maxn], dp[maxn][maxn];
     7 const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
     8 
     9 inline bool in(const int x, const int y){
    10     return x >= 0 && x < r && y >= 0 && y < c;
    11 }
    12 int solve(int x, int y){
    13     if(dp[x][y] != -1) return dp[x][y];
    14     int & ans = dp[x][y];
    15     ans = 1;
    16     for(int i = 0; i < 4; ++i){
    17         int xx = x + dir[i][0], yy = y + dir[i][1];
    18         if(in(xx, yy) && a[xx][yy] > a[x][y])
    19             ans = max(ans, solve(xx, yy) + 1);
    20     }
    21     return ans;
    22 }
    23 int main()
    24 {
    25     int T; cin >> T;
    26     while(T--){
    27         memset(dp, -1, sizeof(dp));
    28         cin >> str >> r >> c;
    29         for(int i = 0; i < r; ++i)
    30             for(int j = 0; j < c; ++j)
    31                 cin >> a[i][j];
    32         int ans = INT_MIN;
    33          for(int i = 0; i < r; ++i)
    34             for(int j = 0; j < c; ++j)
    35                 ans = max(ans, solve(i, j));
    36         printf("%s: %d
    ", str.c_str(), ans);
    37     }
    38     return 0;
    39 }



  • 相关阅读:
    SQL Server如何固定执行计划
    领导修炼
    content management system
    npm和bower
    web开发workflow
    偏执狂
    website project team member 角色及开发过程概念图
    website architecture
    王道霸道
    design pattern及其使用
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312668.html
Copyright © 2011-2022 走看看