JAVA CCF-201403-2 窗口

欢迎访问我的CCF认证解题目录

题目描述

思路过程

每个窗口就是一个矩阵,我们定义一个Rect类来存储坐标和编号

定义一个数组来模拟窗口,下标最大时,为顶层,我们每次都从顶层往底层遍历,如果该点存在于矩形中,则将对应的矩阵移到顶层。

代码

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(), m = in.nextInt();
		LinkedList<Rect> arrs = new LinkedList<Rect>();//存储所有窗口
		for ( int i = 0; i < n; i++ ) arrs.add(new Rect(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), i+1));
		out:
		for ( int i = 0; i < m; i++ ) {
			int x = in.nextInt(), y = in.nextInt();
			//从最顶层遍历
			for ( int j = arrs.size()-1; j >= 0; j-- ) {
				if ( arrs.get(j).isIn(x, y) ) {//在该窗口中
					System.out.println(arrs.get(j).num);
					Rect temp = arrs.remove(j);//换到最顶层
					arrs.add(temp);
					continue out;
				}
			}
			System.out.println("IGNORED");
		}
	}
	
}
//存储坐标和编号
class Rect {
	int x1,y1,x2,y2,num;

	public Rect(int x1, int y1, int x2, int y2, int num) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.num = num;
	}
	//如果在这矩形中
	public boolean isIn( int x, int y ) {
		return ( x >= x1 && x <= x2 && y >= y1 && y <= y2 );
	}
}
发布了60 篇原创文章 · 获赞 0 · 访问量 2152

猜你喜欢

转载自blog.csdn.net/weixin_43732798/article/details/100705095