pku1088
来源:互联网 发布:linux清除文件夹内容 编辑:程序博客网 时间:2024/05/21 17:53
使用DFS算法和备忘录的方式,减少迭代次数。
import java.util.Scanner;public class Main {public static int[][]result;public static void main(String[] args) { Scanner cin=new Scanner(System.in); int r=cin.nextInt(); int c=cin.nextInt(); int [][]heights=new int[r][c]; result=new int[r][c]; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ heights[i][j]=cin.nextInt(); } } int max=0; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ int temp=maxHeight(heights,i,j,r,c); max=temp>max?temp:max; } } System.out.println(max); }public static int maxHeight(int[][]a,int r,int c,int h,int w){if(result[r][c]!=0){return result[r][c];}int lefth = 0,righth=0,toph=0,bottomh=0;if(c>0&&a[r][c-1]<a[r][c]){lefth=maxHeight(a,r,c-1,h,w)+1;}if(c<(w-1)&&a[r][c+1]<a[r][c]){righth=maxHeight(a,r,c+1,h,w)+1;}if(r>0&&a[r-1][c]<a[r][c]){toph=maxHeight(a,r-1,c,h,w)+1;}if(r<(h-1)&&a[r+1][c]<a[r][c]){bottomh=maxHeight(a,r+1,c,h,w)+1;}int maxw=lefth>righth?lefth:righth;int maxh=toph>bottomh?toph:bottomh;if(maxh==0&&maxw==0){result[r][c]=1;return 1;}result[r][c]=maxw>maxh?maxw:maxh;return maxw>maxh?maxw:maxh;}}
0 0
- pku1088
- pku1088题解
- PKU1088 滑雪
- PKU1088---记忆化搜索
- pku1088滑雪 记忆搜索
- PKU1088 记忆化搜索
- pku1088 滑雪(记忆化搜索)
- 北大 C++ 8.4 list and deque
- 学以致用
- Netease
- Spring源码分析3----SpringMVC的设计与实现和视图的呈现
- Python中time模块详解
- pku1088
- 第3题:Longest Substring Without Repeating Characters
- js与jq处理盒子模型&&定位
- 多线程与多进程
- 练习系统 实验三 菲波那契数列
- 【java 类加载的深入研究1】loadClass()的研究
- BaseDialog
- 设计模式学习笔记---设计原则
- 如何快速成为一名Linux运维工程师