View Code of Problem 217

import java.util.*;
class LouTi{
	int hang;
	int lie;
	int high;
	ArrayList<LouTi> link ;	
	public int getway() {
		if(link.size()==0) {
			return 0;
		}
		int step=0;
		int max=0;
		for(LouTi lt : link) {
			step=lt.getway()+1;
			if (max<step) {
				max=step;
			}
		}
		return max;
	}	
} 
public class Main {	
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
			while(in.hasNext()) {
				int n = in.nextInt();
				int m = in.nextInt();
				LouTi[][] AllLouTi = new LouTi[n][m];
				for(int i =0;i<n;i++) {
					for(int j=0;j<m;j++) {
						LouTi lt = new LouTi();
						lt.hang=i;
						lt.lie=j;
						lt.high=in.nextInt();
						lt.link=new ArrayList<LouTi>();
						AllLouTi[i][j] = lt;
					}
				}
				for(int i =0;i<n;i++) {
					for(int j=0;j<m;j++) {
						try {
							if(AllLouTi[i+1][j].high<AllLouTi[i][j].high){
								AllLouTi[i][j].link.add(AllLouTi[i+1][j]);
							}			
						}catch(Exception ex) {
						}
						try {
							if(AllLouTi[i][j+1].high<AllLouTi[i][j].high){
								AllLouTi[i][j].link.add(AllLouTi[i][j+1]);
							}						
						}catch(Exception ex) {		
						}
						try {
							if(AllLouTi[i-1][j].high<AllLouTi[i][j].high){
								AllLouTi[i][j].link.add(AllLouTi[i-1][j]);
							}					
						}catch(Exception ex) {		
						}
						try {
							if(AllLouTi[i][j-1].high<AllLouTi[i][j].high){
								AllLouTi[i][j].link.add(AllLouTi[i][j-1]);
							}							
						}catch(Exception ex) {		
						}
					}
				}
				
				int MAX = 0;
				for(int i =0;i<n;i++) {
					for(int j=0;j<m;j++) {
						if(AllLouTi[i][j].getway()>MAX) {
							MAX=AllLouTi[i][j].getway();
						}
					}
				}
				System.out.println(MAX+1);
			}
	}
}

Double click to view unformatted code.


Back to problem 217