1712 - 滑雪
Time Limit : 1 秒
Memory Limit : 128 MB
Michael喜欢滑雪这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Examples
Input Format
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Output Format
25
Solution C
#include "stdio.h" int a[101][101],b[101][101],d[101][101]; int m,n; int max; int F(int i,int j) { if(b[i][j]) return d[i][j]; if(i>1&&a[i][j]>a[i-1][j]) d[i][j]=d[i][j]>F(i-1,j)+1?d[i][j]:F(i-1,j)+1; if(i<m&&a[i][j]>a[i+1][j]) d[i][j]=d[i][j]>F(i+1,j)+1?d[i][j]:F(i+1,j)+1; if(j>1&&a[i][j]>a[i][j-1]) d[i][j]=d[i][j]>F(i,j-1)+1?d[i][j]:F(i,j-1)+1; if(j<m&&a[i][j]>a[i][j+1]) d[i][j]=d[i][j]>F(i,j+1)+1?d[i][j]:F(i,j+1)+1; b[i][j]=1; return d[i][j]; } int main() { int i,j; scanf("%d%d",&m,&n); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); b[i][j]=0; d[i][j]=1; } max=1; for(i=1;i<=m;i++) for(j=1;j<=n;j++) if(max<F(i,j)) max=F(i,j); printf("%d\n",max); }
Solution C++
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int dx[4]={-1, 1, 0, 0}, dy[4]={ 0, 0,-1, 1}; struct node{ int x,y,h;}; node a[10005]; int map[105][105],len[105][105]={0}; bool comp(const node &x,const node &y) { return x.h<y.h; } void dp(int i) { int x0,y0,x1,y1; x0=a[i].x; y0=a[i].y; for (int j=0;j<4;j++) { x1=x0+dx[j]; y1=y0+dy[j]; if (map[x1][y1]<map[x0][y0]&&len[x1][y1]>len[x0][y0]) len[x0][y0]=len[x1][y1]; } len[x0][y0]++; } int main() { // ifstream cin("ski.in"); // ofstream cout("ski.out"); int row,col; while (cin>>row>>col) { int k=0; for (int i=1;i<=row;i++) for (int j=1;j<=col;j++) { cin>>map[i][j]; a[k].x=i; a[k].y=j; a[k++].h=map[i][j]; } sort(a,a+k,comp); memset(len,0,sizeof(len)); for (int i=0;i<k;i++) dp(i); int ans(0); for (int i=1;i<=row;i++) for (int j=1;j<=col;j++) if (len[i][j]>ans) ans=len[i][j]; cout<<ans<<endl; } // system("pause"); return 0; }
Solution Java
import java.util.Scanner; public class Main { static final int M = 105; static int[][] matrix = new int[M][M]; static int[][] dp = new int[M][M]; static int r,c; public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextInt()){ r = in.nextInt(); c = in.nextInt(); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ matrix[i][j] = in.nextInt(); } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ maxLength(i,j); } } int max = -1; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if(max<dp[i][j]) max = dp[i][j]; } } System.out.println(max); } } public static int max(int a, int b, int c, int d) { int m1 = a > b ? a : b; int m2 = c > d ? c : d; return (m1 > m2 ? m1 : m2); } public static int maxLength(int i, int j) { if (dp[i][j] > 0) return dp[i][j]; int a = 0, b = 0, c = 0, d = 0; if (i - 1 >= 0 && matrix[i][j] > matrix[i - 1][j]) a = matrix[i - 1][j]; if (i + 1 < r && matrix[i][j] > matrix[i + 1][j]) b = matrix[i + 1][j]; if (j - 1 >= 0 && matrix[i][j] > matrix[i][j - 1]) c = matrix[i][j - 1]; if (j + 1 < c && matrix[i][j] > matrix[i][j + 1]) d = matrix[i][j + 1]; dp[i][j] = max(a, b, c, d) + 1; return dp[i][j]; } }