POJ1088

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1014

思路:DP,走过一个点,那么记住如果从这个点开始的最大长度,减少冗余。

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class Main {
  5.     int[][] h;
  6.     int[][] used;
  7.     int max;
  8.     public static void main(String[] args) throws IOException {
  9.         new Main();
  10.     }
  11.     public Main() throws IOException {
  12.         BufferedReader read = new BufferedReader(new InputStreamReader(
  13.                 System.in));
  14.         String[] s;
  15.         s = read.readLine().split(" ");
  16.         int r = Integer.parseInt(s[0]);
  17.         int c = Integer.parseInt(s[1]);
  18.         h = new int[r][c];
  19.         used = new int[r][c];
  20.         max = 0;
  21.         for (int i = 0; i < r; i++) {
  22.             s = read.readLine().split(" ");
  23.             for (int j = 0; j < c; j++) {
  24.                 h[i][j] = Integer.parseInt(s[j]);
  25.             }
  26.         }
  27.         for (int i = 0; i < r; i++) {
  28.             for (int j = 0; j < c; j++) {
  29.                 int tmax = 0;
  30.                 tmax = move(i, j);
  31.                 if (tmax > max) {
  32.                     max = tmax;
  33.                 }
  34.             }
  35.         }
  36.         System.out.println(max);
  37.     }
  38.     public int move(int r, int c) {
  39.         if (used[r][c] != 0) {
  40.             return used[r][c];
  41.         } else {
  42.             int tmax = 1;
  43.             int t;
  44.             if (r - 1 >= 0 && h[r][c] > h[r - 1][c]) {
  45.                 t = move(r - 1, c);
  46.                 if (t + 1 > tmax) {
  47.                     tmax = t + 1;
  48.                 }
  49.             }
  50.             if (r + 1 < h.length && h[r][c] > h[r + 1][c]) {
  51.                 t = move(r + 1, c);
  52.                 if (t + 1 > tmax) {
  53.                     tmax = t + 1;
  54.                 }
  55.             }
  56.             if (c - 1 >= 0 && h[r][c] > h[r][c - 1]) {
  57.                 t = move(r, c - 1);
  58.                 if (t + 1 > tmax) {
  59.                     tmax = t + 1;
  60.                 }
  61.             }
  62.             if (c + 1 < h[0].length && h[r][c] > h[r][c + 1]) {
  63.                 t = move(r, c + 1);
  64.                 if (t + 1 > tmax) {
  65.                     tmax = t + 1;
  66.                 }
  67.             }
  68.             used[r][c] = tmax;
  69.             return tmax;
  70.         }
  71.     }
  72. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值