CCF之窗口(java)

来源:互联网 发布:html5 商城源码下载 编辑:程序博客网 时间:2024/04/29 18:55

试题编号:201403-2试题名称:窗口时间限制:1.0s内存限制:256.0MB问题描述:

问题描述
  在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域。窗口的边界上的点也属于该窗口。窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的内容。
  当你点击屏幕上一个点的时候,你就选择了处于被点击位置的最顶层窗口,并且这个窗口就会被移到所有窗口的最顶层,而剩余的窗口的层次顺序不变。如果你点击的位置不属于任何窗口,则系统会忽略你这次点击。
  现在我们希望你写一个程序模拟点击窗口的过程。
输入格式
  输入的第一行有两个正整数,即 N 和 M。(1 ≤ N ≤ 10,1 ≤ M ≤ 10)
  接下来 N 行按照从最下层到最顶层的顺序给出 N 个窗口的位置。 每行包含四个非负整数 x1, y1, x2, y2,表示该窗口的一对顶点坐标分别为 (x1, y1) 和 (x2, y2)。保证 x1 < x2,y1 2。
  接下来 M 行每行包含两个非负整数 x, y,表示一次鼠标点击的坐标。
  题目中涉及到的所有点和矩形的顶点的 x, y 坐标分别不超过 2559 和  1439。
输出格式
  输出包括 M 行,每一行表示一次鼠标点击的结果。如果该次鼠标点击选择了一个窗口,则输出这个窗口的编号(窗口按照输入中的顺序从 1 编号到 N);如果没有,则输出"IGNORED"(不含双引号)。
样例输入
3 4
0 0 4 4
1 1 5 5
2 2 6 6
1 1
0 0
4 4
0 5
样例输出
2
1
1
IGNORED
样例说明
  第一次点击的位置同时属于第 1 和第 2 个窗口,但是由于第 2 个窗口在上面,它被选择并且被置于顶层。
  第二次点击的位置只属于第 1 个窗口,因此该次点击选择了此窗口并将其置于顶层。现在的三个窗口的层次关系与初始状态恰好相反了。
  第三次点击的位置同时属于三个窗口的范围,但是由于现在第 1 个窗口处于顶层,它被选择。
  最后点击的 (0, 5) 不属于任何窗口。

解题代码(java):

方法一:

import java.util.*;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int N = in.nextInt();int M = in.nextInt();ArrayList<window> list = new ArrayList<window>();for (int i = 0; i < N; i++) {list.add(new window(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), i + 1));}int x0 = 0, y0 = 0;String out[] = new String[M];int k;for (int j = 0; j < M; j++) {x0 = in.nextInt();y0 = in.nextInt();k = N - 1;for (; k >= 0; k--) {if (list.get(k).check(x0, y0) != 0) {out[j] = list.get(k).check(x0, y0) + "";list.add(list.get(k));list.remove(k);break;}}if (k < 0)out[j] = "IGNORED";}for (int l = 0; l < M; l++) {System.out.println(out[l]);}}static class window {int x1, x2, y1, y2;int order;public window(int x1, int y1, int x2, int y2, int order) {this.x1 = x1;this.y1 = y1;this.x2 = x2;this.y2 = y2;this.order = order;}public int check(int x0, int y0) {if (x0 >= x1 && x0 <= x2 && y0 >= y1 && y0 <= y2) {return order;} else {return 0;}}}}
方法二:

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int N = scanner.nextInt();int M = scanner.nextInt();int[][] coord = new int[N][5];for (int i = 0; i < N; i++) {coord[i][0] = i + 1;for (int j = 1; j < 5; j++) {coord[i][j] = scanner.nextInt();}}int result[]=new int[M];int[]arr=new int[5];for(int i = 0; i < M; i++) {int x = scanner.nextInt();int y = scanner.nextInt();for (int j = N - 1; j >= 0; j--) {//从最后一行搜索if (x >= coord[j][1] && x <= coord[j][3] && y >= coord[j][2] && y <= coord[j][4]) {result[i]=coord[j][0];for(int k=0;k<5;k++){arr[k] = coord[j][k];}for(int m=j;m<N-1;m++){for(int k=0;k<5;k++){coord[m][k]=coord[m+1][k];}}for(int k=0;k<5;k++){coord[N-1][k]=arr[k];}break;}else{result[i]=0;}}}for (int i = 0; i < M; i++) {if (result[i] == 0) {System.out.println("IGNORED");} else {System.out.println(result[i]);}}}}



0 0
原创粉丝点击