华为笔试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;
    }
}

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

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

相关推荐

每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
1
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务