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然后判断其它点是否可达即可. 原因太简单了.... ...
随机推荐
- RFID基础知识
BS:BinarySearch. TSA:TimeSlottedAloha. BSA:基本二进制搜索算法. DBSA:动态二进制搜索算法. RBSA:后退式二进制搜索算法. FSA:Frame Slo ...
- 利用统计学知识为android应用的启动时间做数据分析
[声明:如需转载本文,请注明来源] 一.数据说明 启动时间用同一台设备,同一个包进行启动时间的测试,其中三组样本数据(每组100份对比数据)如下: 设备pro-5-1 base_list_1 = [0 ...
- switch2osm使用open street map离线地图中文乱码方框解决办法
----------written by shenwenkai------------- ubuntu linux环境下,按照网址(https://switch2osm.org/serving-til ...
- Protocol in Objective-C
Objecttive-C Protocal 相似 Java Interface
- ssh生成key不交互
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" 首次执行不交互 第二次再次执行会让输入y
- JAVA CDI 学习(2) - Scope 生命周期
在上一节中,我们已经知道了如何用@Inject实现基本注入,这一节研究Bean实例注入后的“生命周期”,web application中有几种基本的生命周期(不管哪种编程语言都类似) 1.Applic ...
- 读懂IL代码就这么简单 (一)
一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不知所云,只听高手们说,了解IL代码你能更加清楚的知道你的代码是如何运行相互调用的,此言一出不明觉 ...
- APP架子迁移指南(一)
搭架子是脑垂体在放烟花 俗话说吃多少饭,走多少路,上学的时候捧着<设计模式>就想睡觉,现在轮子看得多了,自然有心领神会之感.搭架子就像谈哲学,如高山流水,遇弯则急.遇潭则深.我印象最深的是 ...
- Lucene.Net的服务器封装+APi组件 (开源)
为什么要封装 真不知道用什么标题合适,我这几天在研究Lucene.Net,觉得把Lucene.Net封装为一个独立的服务器,再提供一个给客户端调用的Api组件应该是一件很意思的事,主要优势有以下: 1 ...
- [CF#290 Div.1 C]Fox And Dinner(最大流)
题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...