1. 程式人生 > 其它 >華為校招機測(8-25)

華為校招機測(8-25)

第一題: 最大值子矩陣

題目給出一個矩陣,裡面的值有正有負,

求一個子矩陣,使得這個子矩陣框住的值最大,並返回最大值。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();//行數
        int cols = sc.nextInt();//列數
        int res = Integer.MIN_VALUE;
        
int[][]matrix = new int[rows][cols]; for(int i=0; i<=rows-1; ++i){ for(int j=0; j<=cols-1; ++j){ matrix[i][j] = sc.nextInt(); } } for(int begin = 0; begin<=rows-1; ++begin){//開始 int[] ring = new int [cols]; int
[] dp = new int[cols]; for(int end = begin; end<= rows-1; ++end){//結束 for(int j=0; j<cols; ++j) ring[j] += matrix[end][j];//列元素和 res = Math.max(res,ring[0]); dp[0] = ring[0]; for(int j=1; j<cols; ++j){
if(dp[j-1]<0)dp[j] = ring[j]; else dp[j] = dp[j-1] + ring[j]; res = Math.max(res,dp[j]);//取最大 } } } System.out.println(res); } } //3 4 //-3 5 -1 5 //2 4 -2 4 //-1 3 -1 3

第二題: 逃跑路徑長度

給出一個矩陣,裡面是秒數,

從左上出發,到右下結束,每走一格,所有秒數都減一

求逃跑路徑的長度(路徑長度==逃跑總時間,一秒一格)

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Node{
    int x;
    int y;
    int hop;
    public Node(int x, int y, int hop) {
        this.x = x;
        this.y = y;
        this.hop = hop;
    }
}

public class Main {
    static int row,col;
    static int[] dx={-1,1,0,0};
    static int[] dy={0,0,1,-1};

    private static int getAns(int[][] grid){
        if (grid[0][0]<=0){
            return -1;
        }
        int[][] minTime=new int[row][col];
        for (int i = 0; i < row; i++) {
            Arrays.fill(minTime[i],Integer.MAX_VALUE);
        }
        Queue<Node> queue=new LinkedList<>();
        queue.add(new Node(0,0,0));
        while (!queue.isEmpty()){
            Node node=queue.poll();
            int x=node.x;
            int y=node.y;
            int hop=node.hop;
//            System.out.println(x+" "+y+" "+hop);
            if (minTime[x][y]<=hop||grid[x][y]<hop)continue;
            minTime[x][y]=hop;
            if (x==row-1&&y==col-1)return hop;
            for (int i = 0; i < dx.length; i++) {
                int nextX=dx[i]+x;
                int nextY=dy[i]+y;
                if (!inGrid(nextX,nextY))continue;
                queue.add(new Node(nextX,nextY,hop+1));
            }
        }
        return -1;
    }
    private static boolean inGrid(int x,int y){
        return x>=0&&x<row&&y>=0&&y<col;
    }

    //Main主函式
    public static void main(String[] args) {
        Scanner s=new Scanner(new BufferedInputStream(System.in));
        row=s.nextInt();
        col=s.nextInt();
        int[][] grid=new int[row][col];
        for (int i = 0; i <row ; i++) {
            for (int j = 0; j < col; j++) {
                grid[i][j]=s.nextInt();
            }
        }
        System.out.println(getAns(grid));
    }
}
//如果想快速取分,直接return row+col-2, 可以得到90%的分

第三題: 任務規劃(AOV)

題目給出多個任務,包括每個任務需要的時間,

以及所有任務依賴的其他任務,依賴的其他任務完成後才能執行本任務,

求完成所有任務所需的時間,如果任務迴圈依賴等待則返回-1

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        ArrayList<HashSet<Integer>> task = new ArrayList<HashSet<Integer>>();//存task任務依賴
        ArrayList<Integer> time = new ArrayList<>();//存時間
        for(int i=0; i<=num-1; ++i){
            String str1 = sc.next();
            String str2 = sc.next();
            String[] strs = str1.split(",");
            HashSet<Integer> task1 = new HashSet<>();
            task.add(task1);

            for(String s:strs){
                task.get(i).add(Integer.valueOf(s));
            }
            //任務
            time.add(Integer.valueOf(str2));
        }

        HashSet<Integer> finish = new HashSet<>();//完成的任務
        finish.add(-1);
        HashSet<Integer> run = new HashSet<>();

        int res = 0;

        for(;res<=1000;res++){
            //檢查finish
            for(Integer f:finish){
                for(int i=0;i<=num-1;++i){
                    task.get(i).remove(f);
                }
            }
            for(int i=0;i<=num-1;++i){
                if(task.get(i).size()==0)run.add(i);
            }
            for(Integer r:run){
                int newValue = time.get(r)-1;
                time.set(r,newValue);
                if(time.get(r)==0)finish.add(r);
            }
            if(finish.size()==num+1){
                res++;
                break;
            }
        }
        if(res == 1001)res =-1;
        System.out.println(res);
    }
}
//6
//3,5 2
//5 3
//4 5
//1 2
//0 3
//-1 1

////16



//3
//-1 1
//2 2
//1 3

////-1


//3
//-1 1
//-1 2
//-1 3

////3