結果
| 問題 |
No.2639 Longest Increasing Walk
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-02-29 11:30:14 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 303 ms / 2,000 ms |
| コード長 | 2,428 bytes |
| コンパイル時間 | 1,842 ms |
| コンパイル使用メモリ | 78,832 KB |
| 実行使用メモリ | 85,768 KB |
| 最終ジャッジ日時 | 2024-09-29 12:40:13 |
| 合計ジャッジ時間 | 7,865 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
static int[][] dp;
static int[][] field;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int h = sc.nextInt();
int w = sc.nextInt();
field = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
field[i][j] = sc.nextInt();
}
}
dp = new int[h][w];
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (dp[i][j] == 0) {
dp[i][j] = dfw(i, j);
}
ans = Math.max(ans, dp[i][j]);
}
}
System.out.println(ans);
}
static int dfw(int r, int c) {
if (dp[r][c] == 0) {
int current = 0;
if (r > 0 && field[r - 1][c] < field[r][c]) {
current = Math.max(current, dfw(r - 1, c));
}
if (r < field.length - 1 && field[r + 1][c] < field[r][c]) {
current = Math.max(current, dfw(r + 1, c));
}
if (c > 0 && field[r][c - 1] < field[r][c]) {
current = Math.max(current, dfw(r, c - 1));
}
if (c < field[r].length - 1 && field[r][c + 1] < field[r][c]) {
current = Math.max(current, dfw(r, c + 1));
}
dp[r][c] = current + 1;
}
return dp[r][c];
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten