3.30 华为笔试 软件岗第一题第二题过

第二题是一个bfs路径搜索,记得剪枝
import java.util.*;

public class Main {
    static int m,n;
    static int[][] map;
    static int[] start;
    static int[] end;
    static int mindis = Integer.MAX_VALUE;
    static int count = 0;
    static int[][] turn = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        map = new int[m][n];
        start = new int[3];
        start[0] = scanner.nextInt();
        start[1] = scanner.nextInt();
        start[2] = 0;
        end = new int[3];
        end[0] = scanner.nextInt();
        end[1] = scanner.nextInt();
        end[2] = 0;
        int k = scanner.nextInt();
        for(int i = 0;i<k;i++){
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            map[x][y] = -1;
        }
        BFS();
        System.out.print(count);
        System.out.print(' ');
        System.out.print(mindis);
    }
    public static void BFS(){
        Queue<int[]> queue = new LinkedList<>();
        queue.add(start);
        while (!queue.isEmpty()){
            int[] cur = queue.poll();
            int x = cur[0];
            int y = cur[1];
            int step = cur[2];
            if(x == end[0] && y == end[1]){
                if(step < mindis){
                    mindis = step;
                    count = 1;
                }
                else if(step == mindis){
                    count ++;
                }
                continue;
            }
            map[x][y] = 1;
            if(step >= mindis) continue;
            for(int i=0;i<4;i++){
                int next_x = x + turn[i][0];
                int next_y = y + turn[i][1];
                int next_step = step + 1;
                if(next_x>=0 && next_x<m && next_y>=0 && next_y<n && map[next_x][next_y]==0){
                    queue.add(new int[]{next_x,next_y,next_step});
                }
            }
        }
    }
}
第一题就是个模拟,两个整数维护AB对应的芯片标号即可,记得在切换芯片的时候把tag置为0(代码写的很丑,可以自行优化一下)
对了这个题有个坑,就是输入是包含空格的,一开始以为没有空格,0了好几把才get这里还有空格(手动汗一个。。。)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = -1;
        int a_tag = 0;
        int b = -1;
        int b_tag = 0;
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        String[] s = new String[n];
        for(int i = 0;i<n;i++){
            s[i] = scanner.next();
        }
        for(int i=0;i<n;i++){
            if(s[i].equals("A")){
                if(a==-1){
                    a = b+1;
                    a_tag++;
                }else{
                    if(a_tag < 4){
                        a_tag++;
                    }
                    else{
                        a = Math.max(a,b) + 1;
                        a_tag = 0;
                        a_tag++;
                    }
                }
            }else{
                if(b==-1){
                    b = a+1;
                    b_tag++;
                }
                else{
                    if(b_tag < 1){
                        b_tag++;
                    }
                    else{
                        b = Math.max(a,b)+1;
                        b_tag = 0;
                        b_tag++;
                    }
                }
            }
        }
        if(s[n-1].equals("A")){
            if(a+1<=m) {
                System.out.println(a + 1);
                System.out.println(a_tag);
            }else{
                System.out.println(0);
                System.out.println(0);
            }
        }else{
            if(b+1<=m) {
                System.out.println(b + 1);
                System.out.println(b_tag);
            }else{
                System.out.println(0);
                System.out.println(0);
            }
        }

    }
}
第三题 这个树着实在我知识盲区 就写了一下建树的过程 旁边的同学好像卡在输入输出了
建完树直接sout(-1) 30分到手,哈哈~


#华为笔试##春招##实习##笔试题目##华为#
全部评论
你投的啥岗呀 约面了吗
1 回复 分享
发布于 2022-04-01 11:42
我也是这三个题目,约了周一面试!目前人好迷惘
点赞 回复 分享
发布于 2022-04-08 16:44
感谢分享。 原来第一题最后输出的时候,是只需要判断最后一个业务用到的芯片编号>m,我是先按 a 和 b 编号都判断了有没有>m,>m输出0 0。 这样就一直只通过90%没找到原因。 对比了一下你的代码,感觉应该是输出理解错了,只管最后一个业务的芯片编号就行,即使 b 超了 m 也不用管 输入: 2 6 A B A B A A 输出: 1 4 如果两个芯片编号都判断 >m 的话就输出 0 0 了😂
点赞 回复 分享
发布于 2022-03-31 16:52

相关推荐

牛客76783384...:字节:不要放箭,活捉赵子龙
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
6
42
分享

创作者周榜

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