zoukankan      html  css  js  c++  java
  • UVa 10285 最长的滑雪路径(DAG上的最长路)

    https://vjudge.net/problem/UVA-10285

    题意:

    在一个R*C的整数矩阵上找一条高度严格递减的最长路。起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外。

    思路:

    DAG上的最长路问题。由于起点不固定,我们每个点都需要试一遍。

     1 #include<iostream> 
     2 #include<string>
     3 #include<cstring>
     4 #include<sstream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 
     9 const int maxn = 100 + 5;
    10 
    11 int n, m;
    12 char s[maxn];
    13 int map[maxn][maxn];
    14 int d[maxn][maxn];
    15 
    16 int dx[] = { 0, 0, 1, -1 };
    17 int dy[] = { 1, -1, 0, 0 };
    18 
    19 
    20 int dp(int i,int j)
    21 {
    22     int& ans = d[i][j];
    23     if (ans > 0)     return ans;
    24     ans = 1;
    25     for (int k = 0; k < 4; k++)
    26     {
    27         int x = i + dx[k];
    28         int y = j + dy[k];
    29         if (x<1 || x>n || y<1 || y>m)     continue;
    30         if (map[x][y] < map[i][j])
    31             ans = max(ans, dp(x,y) + 1);
    32     }
    33     return ans;
    34 }
    35 
    36 int main()
    37 {
    38     //freopen("D:\txt.txt", "r", stdin);
    39     int T;
    40     cin >> T;
    41     while (T--)
    42     {
    43         memset(d, 0, sizeof(d));
    44         cin >> s >> n >> m;
    45         for (int i = 1; i <= n;i++)
    46         for (int j = 1; j <= m; j++)
    47             cin >> map[i][j];
    48 
    49         int maxd = 0;
    50         for (int i = 1; i <= n;i++)
    51         for (int j = 1; j <= m; j++)
    52         {
    53             maxd = max(maxd,dp(i, j));
    54         }
    55         cout << s << ": " << maxd << endl;
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    三 zookeeper集群搭建
    一 linux 基本操作
    linux x64 安装 node
    docker nginx/1.7.4
    搭建Portainer可视化界面
    Swarm搭建 Docker集群
    在 Centos7.4上安装docker
    js 处理json对象数据
    生产者消费者模式及其存在的问题
    多线程
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6376230.html
Copyright © 2011-2022 走看看