最大矩形面积(java)

Problem Description
在一个矩形区域内有很多点,每个点的坐标都是整数。求一个矩形,使之内部没有点,且面积最大。所求矩形的边与坐标轴平行。
Input
一个整数t,表示测试组数。
整数l,w表示矩形横向边长和竖向边长。
 一个整数n,表示该矩形内点的个数。
 n个点的坐标x,y。
Output
最大面积。
Sample Input
2
2 3
0
10 10
4
1 1
9 1
1 9
9 9 
Sample Output
6
80

import java.util.Scanner;

class Point {
	private int x;
	private int y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int count = reader.nextInt();
		for (int i = 0; i < count; i++) {
			Point p1 = new Point(0, 0);
			int l = reader.nextInt();
			int h = reader.nextInt();
			Point p2 = new Point(l, h);
			int pointsCount = reader.nextInt();
			Point[] points = new Point[pointsCount + 2];
			points[0] = p1;
			points[pointsCount + 1] = p2;
			for (int j = 1; j <= pointsCount; j++) {
				int x = reader.nextInt();
				int y = reader.nextInt();
				points[j] = new Point(x, y);
			}
			int area1 = areaFromXDirection(points, l, h);
			int area2 = areaFromYDirection(points, l, h);
			System.out.println(Math.max(area1, area2));
		}
	}

	private static int areaFromYDirection(Point[] points, int l, int h) {
		for (int i = 0; i < points.length - 1; i++) {
			int k = i;
			for (int j = i + 1; j < points.length; j++) {
				if (points[j].getY() < points[k].getY()
						|| (points[j].getY() == points[k].getY()) && (points[j].getX() < points[k].getX())) {
					k = j;
				}
			}
			if (k != i) {
				Point p = points[i];
				points[i] = points[k];
				points[k] = p;
			}

		}
		int maxArea = 0;
		for (int i = 0; i < points.length - 1; i++) {
			int low = 0;
			int high = l;
			int maxY = h - points[i].getY();
			for (int j = i + 1; j < points.length; j++) {
				if (points[j].getX() <= high && points[j].getX() >= low) {
					maxArea = Math.max(maxArea, (points[j].getY() - points[i].getY()) * (high - low));
					if (points[j].getX() == points[i].getX()) {
						break;
					}
					if (points[j].getX() > points[i].getX()) {
						high = points[j].getX();
					} else {
						low = points[j].getX();
					}
					if ((high - low) * maxY < maxArea) {
						break;
					}
				}
			}
		}
		return maxArea;
	}

	private static int areaFromXDirection(Point[] points, int l, int h) {
		for (int i = 0; i < points.length - 1; i++) {
			int k = i;
			for (int j = i + 1; j < points.length; j++) {
				if (points[j].getX() < points[k].getX()
						|| (points[j].getX() == points[k].getX()) && (points[j].getY() < points[k].getY())) {
					k = j;
				}
			}
			if (k != i) {
				Point p = points[i];
				points[i] = points[k];
				points[k] = p;
			}
		}
		int maxArea = 0;
		for (int i = 0; i < points.length - 1; i++) {
			int low = 0;
			int high = h;
			int maxX = l - points[i].getX();
			for (int j = i + 1; j < points.length; j++) {
				if (points[j].getY() <= high && points[j].getY() >= low) {
					maxArea = Math.max(maxArea, (points[j].getX() - points[i].getX()) * (high - low));
					if (points[j].getY() == points[i].getY()) {
						break;
					}
					if (points[j].getY() > points[i].getY()) {
						high = points[j].getY();
					} else {
						low = points[j].getY();
					}
					if ((high - low) * maxX < maxArea) {
						break;
					}
				}
			}
		}
		return maxArea;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值