java滑雪,AcWing 901. 滑雪-java
import java.io.*;
import java.util.Arrays;
class Main {
static int N = 310;
//存儲高度
static int r, c;
static int[][] h = new int[N][N];
//f存儲某點開始下滑的最大長度
static int[][] f = new int[N][N];
//是否已確定最大長度
static boolean[][] flag = new boolean[N][N];
//最大長度
static int res;
static int[] mx = {-1,1,0,0};
static int[] my = {0,0,-1,1};
static boolean check(int x, int y) {
return x < r && y < c && x >= 0 && y >= 0;
}
static void dfs(int x, int y) {
int height = h[x][y];
f[x][y] = 1;
flag[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + mx[i];
int ny = y + my[i];
if (check(nx, ny) && h[x][y] > h[nx][ny]) {
//如果未確定最長滑行距離,進入搜索
if (!flag[nx][ny]) dfs(nx, ny);
f[x][y] = Math.max(f[x][y], f[nx][ny] + 1);
}
}
res = Math.max(res, f[x][y]);
}
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String[] strs = in.readLine().split(" ");
r = Integer.parseInt(strs[0]);
c = Integer.parseInt(strs[1]);
//r行c列
for (int i = 0; i < r; i++) {
strs = in.readLine().split(" ");
for (int j = 0; j < c; j++) h[i][j] = Integer.parseInt(strs[j]);
}
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
if (!flag[i][j]) {
dfs(i,j);
}
}
System.out.println(res);
}
}
總結
以上是生活随笔為你收集整理的java滑雪,AcWing 901. 滑雪-java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 滑雪 洛谷
- 下一篇: 【题解】 [SCOI2012]滑雪