普及练习场 带有技巧的搜索 滑雪

题目链接

题意理解

这就是一个简单的记忆化搜索。

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int[] dx = {0, 0, -1, 1};
    static int[] dy = {-1, 1, 0, 0};
    public static class FastScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.valueOf(nextToken());
        }
    }

    static int R;
    static int C;
    static int[][] cnt;
    static int[][] height;
    static boolean[][] visited;

    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        R = fs.nextInt();
        C = fs.nextInt();
        cnt = new int[R][C];
        height = new int[R][C];
        visited = new boolean[R][C];
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                cnt[i][j] = 1;
                visited[i][j] = false;
                height[i][j] = fs.nextInt();
            }
        }
        int maxLen = -1;
        for(int i = 0; i < R; i++) {
            for(int j = 0; j < C; j++) {
                maxLen = Math.max(maxLen, dfs(i, j));
            }
        }
        System.out.println(maxLen);
    }

    public static int dfs(int x, int y) {
        if(x < 0 || y < 0 || x >= R || y >= C) {
            return 0;
        }
        if(visited[x][y]) {
            return cnt[x][y];
        }
        visited[x][y] = true;
        for(int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(0 <= tx && tx < R && 0 <= ty && ty < C) {
                if(height[x][y] > height[tx][ty]) {
                    cnt[x][y] = Math.max(cnt[x][y], dfs(tx, ty) + 1);
                }
            }

        }
        return cnt[x][y];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值