skiing
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的更多相关文章
- nyoj 10 skiing(记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- NYOJ10,skiing
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向下倾斜,并且 ...
- ACM Skiing问题
ACM Skiing问题 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michae ...
- 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 ...
- POJ 3037 Skiing(如何使用SPFA求解二维最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-3037 Bessie and the rest of Farmer John's cows are taking a ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing (拓扑排序+假dp)
题目链接:https://nanti.jisuanke.com/t/16957 题目: In this winter holiday, Bob has a plan for skiing at the ...
- NYOJ 10 skiing(好题)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- NYOJ 10 skiing (深搜和动归)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...
- 【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3432 题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可. 原因太简单了.... ...
随机推荐
- java 22 - 6 多线程之线程调度和设置线程的优先级
线程调度 假如我们的计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到 CPU时间片,也就是使用权,才可以执行指令. 那么Java是如何对线程进行调用的呢? 线程有两种调 ...
- CSS3之文本阴影text-shadow
- Distribute numbers to two “containers” and minimize their difference of sum
it can be solved by Dynamical Programming.Here are some useful link: Tutorial and Code: http://www.c ...
- 投入Html5的怀抱,最近在研究的Egret
html5没有办法不关注,实在太火热了,几年前还不行,如今确是环境较好,typescript语言很好学习,可能基于之前的基础,不到一个星期就基本上差不多了,虽然还有一些小问题,但那都是经验积累下来可以 ...
- HP “云图”GPU虚拟化工作站解决方案
HP PCS ”云图”GPU虚拟化工作站解决方案 ——将图形计算从桌面移到数据中心 惠普云图形GPU虚拟化桌面系统是以用户为中心的私有云服务.除了保留了传统桌面虚拟化方案以集中设备为中心统一管理等优点 ...
- [MetaHook] Event Hook
#include <metahook.h> struct event_hook_t { event_hook_t *next; char *name; void (*pfnEvent)(e ...
- sql 2012 提示列名无效 但可以执行问题
笔者目前使用Ctrl+Shift+R可以解决这个问题,因为智能感知的问题,需要重新整理一下intellisense.有其他方法,请园友共享一下,谢谢. VS2012及13都有用到智能感知,而在sql里 ...
- 283 Move Zeroes
/** * 题意:将0挪到末尾,并且不改数组中原有元素的顺序 * 解析:找到0元素,然后寻找其后面非0的元素,进行交换位置 * @param {number[]} nums * @return {vo ...
- Mysql之case语句(附带实例)
这段时间,做项目做累了,好不容易有点个人的学习时间,利用这个小时,总结一下,最近做统计的时候常用的case语句吧. 结构:case when… then …end 1.判断的同时改变其值 eg: ...
- Castle 多继承选择
Castle 多继承选择 很多时候,我们定义了一个接口,但是这个接口会有多种不同的,这时IOC构造函数注入的时候,就需要自动选择对应的实现. public interface ITestService ...