• skiing


     1 package noj_skiing;
     2 
     3 import java.util.*;
     4 import java.math.*;
     5 
     6 public class Main {
     7 
     8     public static void main(String[] args) {
     9         Solution s = new Solution();
    10         Scanner sc = new Scanner(System.in);
    11         int N = sc.nextInt();
    12         int[] result = new int[N];
    13         for(int i = 0; i < N; i++) {
    14             int rowNum = sc.nextInt(); 
    15             int colNum = sc.nextInt();
    16             int[][] info = new int[rowNum][colNum];
    17             for(int r = 0; r < rowNum; r++) {
    18                 for(int c = 0; c < colNum; c++) {
    19                     info[r][c] = sc.nextInt();
    20                 }    
    21             }
    22             result[i] = s.getResult(info, rowNum, colNum);
    23         }
    24         sc.close();
    25         for(int i : result) {
    26             System.out.println(i);
    27         }
    28     }
    29 
    30 }
    31 class Solution {
    32     public int getResult(int[][] info, int rowNum, int colNum) {
    33         int max = 0;
    34         int[][] states = new int[rowNum][colNum]; //make sure equal 0 all
    35         for(int r = 0; r < rowNum; r++) {
    36             for(int c = 0; c < colNum; c++) {
    37                 max = Math.max(max, getMaxLength(info, r, c, rowNum, colNum, states));
    38             }    
    39         }
    40         return max;
    41     }
    42     public int getMaxLength(int[][] info, int row, int col, int rowNum, int colNum, int[][] states) {
    43     
    44         if(row < 0 || row >= rowNum || col < 0 || col >= colNum) //如果输入的坐标越界的处理
    45             return 0;
    46         if(states[row][col] != 0) //所求位置的最大长度求过
    47             return states[row][col];
    48         
    49         int nextStep = findNextStep(info, row, col, rowNum, colNum);
    50         if(nextStep == 0) //所求位置在队尾
    51             return states[row][col] = 1;
    52         int maxLengthUp     = (nextStep & 8) == 8 ? getMaxLength(info, row - 1, col, rowNum, colNum, states) : 0;
    53         int maxLengthDown     = (nextStep & 4) == 4 ? getMaxLength(info, row + 1, col, rowNum, colNum, states) : 0;
    54         int maxLengthLeft     = (nextStep & 2) == 2 ? getMaxLength(info, row, col - 1, rowNum, colNum, states) : 0;
    55         int maxLengthRight     = (nextStep & 1) == 1 ? getMaxLength(info, row, col + 1, rowNum, colNum, states) : 0;
    56                 
    57         return max_4(maxLengthUp, maxLengthDown, maxLengthLeft, maxLengthRight) + 1;
    58     }
    59     public int findNextStep(int[][] info, int row, int col, int rowNum, int colNum) {
    60         int result = 0;
    61         
    62         if(row != 0 && info[row - 1][col] < info[row][col])             //up
    63             result |= 8;
    64         if(row != rowNum - 1 && info[row + 1][col] < info[row][col])     //down
    65             result |= 4;
    66         if(col != 0 && info[row][col - 1] < info[row][col])             //left
    67             result |= 2;
    68         if(col != colNum - 1 && info[row][col + 1] < info[row][col])     //right
    69             result |= 1;
    70         
    71         return result;
    72     }
    73     public int max_4(int i_1, int i_2, int i_3, int i_4) {
    74         return Math.max(Math.max(i_1, i_2), Math.max(i_3, i_4));
    75     }
    76 }

    感觉不是很难,思路一上来就可以想出来,具体一些细节的处理也没有很坑的地方。

  • 相关阅读:
    Qt全局宏和变量
    QT_begin_namespace和QT_end_namespace的作用
    Qt 打开文件的默认路径 QFileDialog::getOpenFileName()
    QT的安装及环境配置
    C/C++文件操作1
    C/C++文件操作2
    AnsiString和String的区别、使用
    字符转换
    C++Builder中MessageBox的基本用法
    Windows 编程中恼人的各种字符以及字符指针类型
  • 原文地址:https://www.cnblogs.com/dsj2016/p/5237275.html
Copyright © 2020-2023  润新知