CCF 201403-2 窗口 Java

根据题目要求,需要经常用到删除和添加操作,所以这题关键采用链表LinkedList这种数据结构来保存窗口位置,把移除后的窗口再放到头部

 

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

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt(); // 窗口数量
		int M = scanner.nextInt(); // 鼠标点击次数
		int[][] click = new int[M][2]; // 记录窗口位置
                //采用链表来保存所有的窗口
		LinkedList<Area> linkedList = new LinkedList<>();
		for (int i = 0; i < N; i++) {
			int x1 = scanner.nextInt();
			int y1 = scanner.nextInt();
			int x2 = scanner.nextInt();
			int y2 = scanner.nextInt();
			Area area = new Area(x1, y1, x2, y2, i + 1);    
			linkedList.push(area);
		}
		for (int i = 0; i < M; i++) {
			click[i][0] = scanner.nextInt();
			click[i][1] = scanner.nextInt();
		}
                

		for (int i = 0; i < M; i++) {
			boolean found = false;//判断是否点击窗口
			for (int j = 0; j < N; j++) {
				Area area = linkedList.get(j);
				int x = click[i][0];
				int y = click[i][1];
				int x1 = area.getX1();
				int y1 = area.getY1();
				int x2 = area.getX2();
				int y2 = area.getY2();
                                //如果点击的点在窗口范围内,则让点击到的窗口移除链表再放到头部
				if (x >= x1 && y >= y1 && x <= x2 && y <= y2) {
					linkedList.remove(j);
					linkedList.push(area);
					System.out.println(area.getIndex());
					found = true;
					break;
				}
			}
			if (!found)
				System.out.println("IGNORED");
		}
		scanner.close();
	}
    
        //定义一个Area类来保存一个窗口的两个点和位置
	private static class Area {
		private int x1;
		private int y1;
		private int x2;
		private int y2;
		private int index;

		public Area(int x1, int y1, int x2, int y2, int index) {
			this.x1 = x1;
			this.y1 = y1;
			this.x2 = x2;
			this.y2 = y2;
			this.index = index;
		}

		public int getIndex() {
			return index;
		}

		public int getX1() {
			return x1;
		}

		public int getY1() {
			return y1;
		}

		public int getX2() {
			return x2;
		}

		public int getY2() {
			return y2;
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值