2019 Alibaba intern client development engineer Android programming problem dirty matrix merger

Record a programming question for the 2019 Alibaba Android development intern who participated tonight.
The other one didn’t even understand the topic, and a lot of time was wasted. Qaq
is actually not very difficult after understanding the topic. I didn’t have time at the time, and I made some mistakes in the rush. Hey, I knew it was so difficult. Now, why bother...
not much to talk about the topic

Topic: Dirty Matrix Merging

In order to record this question, I cut a lot of pictures, don't believe it, it can surprise people by looking at the question. After watching it for a long time, I don't know what a ghost question is.

Dear friends, please experience it~~~

Figure one
Figure one

Picture 2
Figure II
Picture 3
Figure three
Picture 4
Figure Four
Picture 5
Figure 5
Picture 6
Figure Six

Figure Seven
Figure Seven

Ideas

how about it? It will take a long time to understand the question, this is not a guarantee that you can understand QAQ

The meaning of the question I understand is summarized in one sentence: the matrix is ​​merged, and then the area is calculated .

The single rendering time is the area of ​​a rectangle s+10000, that is, t = s + 10000
total rendering time, which is the sum of the rendering time of all merged rectangles.

Code

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Testss {

	 private static class Rect {
	        private int left;
	        private int top;
	        private int width;
	        private int height;
	    }
	public static void main(String[] args) {
		 Scanner in = new Scanner(System.in);
	        int n = Integer.parseInt(in.nextLine());
	        Rect[] rects = new Rect[n];
	        for (int i = 0; i < n; i++) {
	            rects[i] = new Rect();
	            rects[i].left = Integer.parseInt(in.nextLine());
	            rects[i].top = Integer.parseInt(in.nextLine());
	            rects[i].width = Integer.parseInt(in.nextLine());
	            rects[i].height = Integer.parseInt(in.nextLine());
	        }
	        System.out.println(String.valueOf(costTime(rects)));

	}
	/**
	 * 两个矩形是否能合并
	 * @param r1
	 * @param r2
	 * @return
	 */
	public static boolean isMerge(Rect r1,Rect r2){
		int right1 = r1.left+r1.width;//右
		int b1 = r1.top+r1.height;//下
		
		int right2 = r2.left+r2.width;//右
		int b2 = r2.top+r2.height;//下
		//一个矩阵在另一个矩阵的左上右下部分任何一个方位,就是没有交集,就是不能合并
		if(b2<r1.top && right2<r1.left || r2.top>b1 && right2<r1.left
				|| b2<r1.top && r2.left>right1 || r2.top>b1 && r2.left>right1
				){
			return false;
		}
		//否则有交集,能合并
		return true;
		
	}
	/**
	 * 合并两个矩形
	 * @param r1
	 * @param r2
	 * @return
	 */
	public static Rect merge(Rect r1,Rect r2){
		int right1 = r1.left+r1.width;
		int b1 = r1.top+r1.height;
		
		int right2 = r2.left+r2.width;
		int b2 = r2.top+r2.height;
		//合并成新的矩阵
		Rect r = new Rect();
		r.left = r1.left<r2.left?r1.left:r2.left;//左
		r.top = r1.top<r2.top?r1.top:r2.top;//上
		
		int right = right1>right2?right1:right2;//右
		int b = b1>b2?b1:b2;//下
		
		r.width = right-r.left;//宽
		r.height = b-r.top;//高
		
		return r;
		
	}
	
	
	private static long costTime(Rect[] rects) {
        int len = rects.length;
        if(len == 0) return 0;
        
        List<Rect> rec = new ArrayList<>();//暂存列表
        List<Rect> realRec = new ArrayList<>();//真正合并后的矩形
        rec.add(rects[0]);//先添加一个
        for(int i = 1;i<len;i++){
        	boolean isMerged = false;//标记是否合并了
        	//从暂存表中依次比较,看能否合并,
        	for(Rect re:rec){
        		boolean canM = isMerge(re,rects[i]);
        		if(canM){
        		//能删除当前的,并添加合并后的
        			rec.remove(re);
        			rec.add(merge(re,rects[i]));
        			isMerged = true;
        			break;
        		}
        	}
        	//如果出现一个不能合并的,就把暂存合并的矩形放到真正合并后的列表中
        	//开启一轮新的合并
        	if(!isMerged) {
        		//保存真正的合并后的矩形
        		realRec.addAll(rec);
        		//清空暂存列表
        		rec.clear();
        		//添加该不能合并的矩形,开启新的一轮合并
        		rec.add(rects[i]);
        	}
        }
        //添加最后一轮合并好的矩形
        if(rec.size()!=0){
        	realRec.addAll(rec);
        }
        //计算最终面积,结果
        int resu = 0;
		for(Rect r: realRec){
			resu+=r.width*r.height+10000;
		}
        return resu;
    }
}

I don't know if it's right. . .
Anyway, the use case it gave was tested and the result was correct.

Hey, not well done...
Fed up with the hardships of life

Guess you like

Origin blog.csdn.net/fxjzzyo/article/details/89264177