package noj_skiing;

 import java.util.*;
import java.math.*; public class Main { public static void main(String[] args) {
Solution s = new Solution();
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] result = new int[N];
for(int i = 0; i < N; i++) {
int rowNum = sc.nextInt();
int colNum = sc.nextInt();
int[][] info = new int[rowNum][colNum];
for(int r = 0; r < rowNum; r++) {
for(int c = 0; c < colNum; c++) {
info[r][c] = sc.nextInt();
}
}
result[i] = s.getResult(info, rowNum, colNum);
}
sc.close();
for(int i : result) {
System.out.println(i);
}
} }
class Solution {
public int getResult(int[][] info, int rowNum, int colNum) {
int max = 0;
int[][] states = new int[rowNum][colNum]; //make sure equal 0 all
for(int r = 0; r < rowNum; r++) {
for(int c = 0; c < colNum; c++) {
max = Math.max(max, getMaxLength(info, r, c, rowNum, colNum, states));
}
}
return max;
}
public int getMaxLength(int[][] info, int row, int col, int rowNum, int colNum, int[][] states) { if(row < 0 || row >= rowNum || col < 0 || col >= colNum) //如果输入的坐标越界的处理
return 0;
if(states[row][col] != 0) //所求位置的最大长度求过
return states[row][col]; int nextStep = findNextStep(info, row, col, rowNum, colNum);
if(nextStep == 0) //所求位置在队尾
return states[row][col] = 1;
int maxLengthUp = (nextStep & 8) == 8 ? getMaxLength(info, row - 1, col, rowNum, colNum, states) : 0;
int maxLengthDown = (nextStep & 4) == 4 ? getMaxLength(info, row + 1, col, rowNum, colNum, states) : 0;
int maxLengthLeft = (nextStep & 2) == 2 ? getMaxLength(info, row, col - 1, rowNum, colNum, states) : 0;
int maxLengthRight = (nextStep & 1) == 1 ? getMaxLength(info, row, col + 1, rowNum, colNum, states) : 0; return max_4(maxLengthUp, maxLengthDown, maxLengthLeft, maxLengthRight) + 1;
}
public int findNextStep(int[][] info, int row, int col, int rowNum, int colNum) {
int result = 0; if(row != 0 && info[row - 1][col] < info[row][col]) //up
result |= 8;
if(row != rowNum - 1 && info[row + 1][col] < info[row][col]) //down
result |= 4;
if(col != 0 && info[row][col - 1] < info[row][col]) //left
result |= 2;
if(col != colNum - 1 && info[row][col + 1] < info[row][col]) //right
result |= 1; return result;
}
public int max_4(int i_1, int i_2, int i_3, int i_4) {
return Math.max(Math.max(i_1, i_2), Math.max(i_3, i_4));
}
}

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

skiing的更多相关文章

  1. nyoj 10 skiing(记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  2. skiing(搜索+记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  3. NYOJ10,skiing

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向下倾斜,并且 ...

  4. ACM Skiing问题

    ACM Skiing问题 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michae ...

  5. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  6. POJ 3037 Skiing(如何使用SPFA求解二维最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-3037 Bessie and the rest of Farmer John's cows are taking a ...

  7. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing (拓扑排序+假dp)

    题目链接:https://nanti.jisuanke.com/t/16957 题目: In this winter holiday, Bob has a plan for skiing at the ...

  8. NYOJ 10 skiing(好题)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  9. NYOJ 10 skiing (深搜和动归)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...

  10. 【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3432 题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可. 原因太简单了.... ...

随机推荐

  1. poj[2392]space elevator

    Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...

  2. Android中实现如下多语言选择Radiobutton效果

    手边的samsung手机设置多语言的方式一般是点击设置多语言的一栏后进入到多语言选择界面,选择完成之后当前的语言环境用小字方式直接显示在设置多语言栏的下方.另一种选择多语言的方式如上图所示,我也在系统 ...

  3. webpack.optimize.CommonsChunkPlugin插件的使用

    方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin('common.js'), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...

  4. HTML 学习笔记 CSS样式(文本)

    CSS文本属性可以定义文本的外观 通过文本属性 您可以改变文本的颜色 字符间距 文本对齐装饰文本 对文本进行缩进等等. 缩进文本 把web页面上的段落的第一行缩进,这是最常用的文本格式化效果. css ...

  5. 1D1D动态规划优化初步

    再学习一下动态规划的基本优化方法- 首先这篇文章应该大家都看过吧-没看过的自行百度 关于实现的思路文章里都给好了-这篇就主要给一点题目啥的 (P.S. 电脑重装了,如果博客发出来有一些奇怪的问题不要在 ...

  6. swift基本用法-数组array

    数组简单用法 //------------------------------------------------------------------------------ // 1. 数组定义 / ...

  7. 使用NIFTI指令画nii图像

    ❤ 关于几种显示工具 mricro:显示出来的左右脑是反着的: mricroN,SPM,xjview,BrainNetViewer:显示出的左右脑是正确的,并且对于做过仿射变换的图像可以自动识别并且校 ...

  8. C# 6.0

    C# 6.0 的新语法特性   回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都 ...

  9. jinja模版

    实现不同机器的差异化配置                 把apache监听的端口统一改为8080     把配置文件files/httpd.conf 文件做成模版         修改lamp.sl ...

  10. office2016各个版本 以及 解决visio搜索任何都提示无匹配项问题

    http://tieba.baidu.com/p/4089747196 版本:Office 2016 Visio 专业版 32位版文件名:SW_DVD5_Visio_Pro_2016_W32_ChnS ...