华为笔试t2


// We have imported the necessary tool classes.
// If you need to import additional packages or classes, please import here.

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static int ansDfs = 0;
    public static boolean[][] vis;
    public static class Node {
        int x;
        int y;
        boolean inc;
        int deep;
        Node() {}
        Node(int x_, int y_, boolean inc_, int deep_) {
            this.x = x_;
            this.y = y_;
            this.inc = inc_;
            this.deep = deep_;
        }
    }
    public static void main(String[] args) {
        // please define the JAVA input here. For example: Scanner s = new Scanner(System.in);
        // please finish the function body here.
        // please define the JAVA output here. For example: System.out.println(s.nextInt());
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[][] g = new int[n][m];
            for(int i = 0;i < n;i++) {
                for(int j = 0;j < m;j++) {
                    g[i][j] = in.nextInt();
                }
            }
            int ans = 0;
            for(int i = 0;i < n;i++) {
                for (int j = 0;j < m;j++) {
                    ansDfs = 0;
                    vis = new boolean[n][m];
                    dfs(new Node(i, j, true, 0), g);
                    ans = Math.max(ans, ansDfs);

                    ansDfs = 0;
                    vis = new boolean[n][m];
                    dfs(new Node(i, j, false, 0), g);
                    ans = Math.max(ans, ansDfs);
                }
            }
            System.out.println(ans);
        }
    }
    public static void dfs(Node node, int[][] g) {
        int n = g.length;
        int m = g[0].length;
        int x = node.x;
        int y = node.y;
        boolean inc1 = node.inc;
        int deep = node.deep;
        ansDfs = Math.max(ansDfs, deep);
        vis[x][y] = true;
        if(x + 1 < n && !vis[x + 1][y]) {
            if((inc1 && (g[x + 1][y] > g[x][y])) || (!inc1 && (g[x + 1][y] < g[x][y]))) {
                dfs(new Node(x + 1, y, !inc1, deep + 1), g);
            }
        }
        if(x - 1 >= 0 && !vis[x - 1][y]) {
            if((inc1 && (g[x - 1][y] > g[x][y])) || (!inc1 && (g[x - 1][y] < g[x][y]))) {
                dfs(new Node(x - 1, y, !inc1, deep + 1), g);
            }
        }
        if(y + 1 < m && !vis[x][y + 1]) {
            if((inc1 && (g[x][y + 1] > g[x][y])) || (!inc1 && (g[x][y + 1] < g[x][y]))) {
                dfs(new Node(x, y + 1, !inc1, deep + 1), g);
            }
        }
        if(y - 1 >= 0 && !vis[x][y - 1]) {
            if((inc1 && (g[x][y - 1] > g[x][y])) || (!inc1 && (g[x][y - 1] < g[x][y]))) {
                dfs(new Node(x, y - 1, !inc1, deep + 1), g);
            }
        }
        vis[x][y] = false;
    }
}

被暑期华为笔试连续两次打击的没有自信了,分享一下第二题思路,其实不难。希望华子能看看我~

#华为##华为笔试#
全部评论
是什么中庸的那道题吗?
点赞 回复 分享
发布于 2023-09-06 21:48 吉林
woc这么复杂
点赞 回复 分享
发布于 2023-09-06 22:28 新加坡
9.6 华为笔试题解已出
点赞 回复 分享
发布于 2023-09-07 13:40 湖北

相关推荐

克莱恩_莫雷蒂:海康也能跟海光比了嘛
投递海光等公司10个岗位
点赞 评论 收藏
分享
评论
1
8
分享
牛客网
牛客企业服务