File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+
10+ static int N ;
11+ static int [][] forest;
12+ static int [][] dp;
13+
14+ // 상하좌우
15+ static int [] di = {- 1 , 1 , 0 , 0 };
16+ static int [] dj = {0 , 0 , - 1 , 1 };
17+
18+ static int ans = 0 ;
19+
20+ public static void main (String [] args ) throws Exception {
21+ st = new StringTokenizer (br. readLine());
22+ N = Integer . parseInt(st. nextToken());
23+
24+ forest = new int [N ][N ];
25+ dp = new int [N ][N ];
26+
27+ for (int i = 0 ; i < N ; i++ ) {
28+ st = new StringTokenizer (br. readLine());
29+ for (int j = 0 ; j < N ; j++ ) {
30+ forest[i][j] = Integer . parseInt(st. nextToken());
31+ }
32+ }
33+
34+ for (int i = 0 ; i < N ; i++ ) {
35+ for (int j = 0 ; j < N ; j++ ) {
36+ ans = Math . max(ans, DFS (i, j));
37+ }
38+ }
39+
40+ bw. write(ans + " " );
41+ bw. close();
42+ }
43+
44+ public static int DFS (int i , int j ) {
45+ if (dp[i][j] != 0 ) {
46+ return dp[i][j];
47+ }
48+
49+ dp[i][j] = 1 ;
50+
51+ for (int k = 0 ; k < 4 ; k++ ) {
52+ int ni = i + di[k];
53+ int nj = j + dj[k];
54+
55+ if (ni >= 0 && ni < N && nj >= 0 && nj < N && forest[ni][nj] > forest[i][j]) {
56+ dp[i][j] = Math . max(dp[i][j], DFS (ni, nj) + 1 );
57+ }
58+ }
59+
60+ return dp[i][j];
61+ }
62+ }
63+ ```
You can’t perform that action at this time.
0 commit comments