简体   繁体   English

Java 2D最小化

[英]Java 2D Minimization

I need help writing a program that is given a specified number of coordinate points in the form (X,Y). 我需要编写一个以(X,Y)形式指定给定坐标点数的程序的帮助。 The number of points that will be given is the first line in the program; 给出的点数是程序的第一行; it can be read in through a scanner. 可以通过扫描仪读取它。

I need to calculate the least amount of area to cover all of the points with the lines x = a, and y = b. 我需要计算最少的面积以覆盖线x = a和y = b的所有点。 So, the area would be a * b (Area of a rectangle). 因此,面积将为* * b(矩形的面积)。

However, one coordinate point (X, Y) must be removed to optimize the area. 但是,必须删除一个坐标点(X,Y)以优化区域。 The point that is removed should minimize the area as much as possible. 删除的点应尽可能减小面积。 I need help writing algorithm to do so. 我需要编写算法的帮助。

This is a sample input, and output that I was given :: 这是一个示例输入,给出的输出是::

SAMPLE INPUT 样品输入

4 4

2 4 2 4

1 1 1 1

5 2 5 2

17 25 17 25

SAMPLE OUTPUT 样品输出

12 12


In this example, the first line of input (4) indicates that four points will be input. 在此示例中,输入(4)的第一行指示将输入四个点。 The next four lines are the coordinates in form (x, y). 接下来的四行是(x,y)形式的坐标。 The last point which is (17, 25) is removed as the outlier which leaves us with the first three points. 最后一个点(17,25)被作为离群值移除,使我们剩下前三个点。

如果剩下的三个点都画了

If the three remaining points are graphed, they can all be inside a box (3 by 4) hence the output is 12; 如果将其余三个点都绘制成图形,则它们都可以在一个方框内(3乘4),因此输出为12; (3 * 4). (3 * 4)。 It is OK for the line to be on the point like in this example. 像本示例中的那样,将直线对准点就可以了。 However, the outlier is not always the last point, or very big. 但是,离群值并不总是最后一点,或者很大。 The outlier could be very small, the area just needs to be minimized. 离群值可能很小,仅需要最小化面积。

-- This is what I have so far (I know it's not very much..) - please help me! -这是我到目前为止所拥有的(我知道不是很多..)-请帮助我!

It's mostly just the algorithm that I need help with.. 这主要只是我需要帮助的算法。

import java.io.*;
import java.util.*;

public class Area {

    public static void main(String args[]) {

        Scanner scan = new Scanner(System.in);

        int numOfPoints = scan.nextInt();
        int Xcoordinates[] = new int[numOfPoints];
        int Ycoordinates[] = new int[numOfPoints];


        for (int i = 0; i <= numOfCows - 1; i++) {
            Xcoordinates[i] = scan.nextInt();
            Ycoordinates[i] = scan.nextInt();
        }

The bruteforce solution is of course to compute the area of every combination of points and select the minimum. 当然,蛮力解决方案是计算点的每个组合的面积并选择最小值。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int numOfPoints = scan.nextInt();
    int[][] points = new int[numOfPoints][2];

    for (int i = 0; i < numOfPoints; i++) {
        points[i][0] = scan.nextInt();
        points[i][1] = scan.nextInt();
    }

    // Testcase, comment everything above out.
    /*
     * int numOfPoints = 4; int[][] points = { { 2, 4 }, { 1, 1 }, { 5, 2 },
     * { 17, 25 } };
     */

    // As we try to minimize, we start with the biggest possible value.
    int minArea = Integer.MAX_VALUE;
    // We don't know which one to skip yet, so this value should be anything
    // *but* a valid value.
    int skippedPoint = -1;
    // Pretty straightforward. Check every point, get minimum
    for (int skipped = 0; skipped < numOfPoints; skipped++) {
        int area = calculateArea(points, skipped);
        if (area < minArea) {
            skippedPoint = skipped;
            minArea = area;
        }
    }

    System.out.println("The original area was " + calculateArea(points, -1) + " units big.");
    System.out.println("The minimized area is " + minArea + " units big.");
    System.out.println("This was reached by leaving the " + (skippedPoint + 1) + ". point (" + Arrays.toString(points[skippedPoint]) + ") out.");
}

/**
 * Implementation of Rajeev Singh's AABB-algorithm
 * 
 * @param points
 *            All points
 * @param skipped
 *            The point to skip
 * @return The area of the axis-aligned bounding box of all points without
 *         the specified point
 */
private static int calculateArea(int[][] points, int skipped) {
    // Initialize values with the opposite of the desired result, see
    // minimization-problem above.
    int max_x = Integer.MIN_VALUE, min_x = Integer.MAX_VALUE, max_y = Integer.MIN_VALUE, min_y = Integer.MAX_VALUE;

    for (int i = 0; i < points.length; i++) {
        if (i == skipped) {
            continue; // This is where the magic happens. Continue
                        // immediatly jumps to the start of the loop.
        }

        int[] point_i = points[i];
        if (point_i[0] > max_x) {
            max_x = point_i[0];
        }
        if (point_i[0] < min_x) {
            min_x = point_i[0];
        }

        if (point_i[1] > max_y) {
            max_y = point_i[1];
        }
        if (point_i[1] < min_y) {
            min_y = point_i[1];
        }
    }

    return (max_x - min_x) * (max_y * min_y);
}

You now have the minimal area and the point that has been left out. 现在,您将拥有最小的面积和已被忽略的点。

Let you have 4 points (2 4), (1 1), (5 2), (17 25) . 让您有4个点(2 4),(1 1),(5 2),(17 25) As you can always remove one point to optimize area hence,there are C(4,3) combinations possible of points,which are: 由于您总是可以删除一个点来优化面积,因此,可能存在点的C(4,3)组合,它们是:

{ { (2 4), (1 1), (5 2) } , { (1 1), (5 2), (17 25) } , { (2 4), (5 2),(17 25) } , { (2 4),(1 1),(17 25) } } { {(2 4),(1 1),(5 2)}{(1 1),(5 2),(17 25)}{(2 4),(5 2),(17 25) }{(2 4),(1 1),(17 25)} }


Minimum area you can find for a set will be: 一套的最小面积为:

(Max(all x coordinates)-Min(all x coordinates)) * (Max(all y coordinates)-Min(all y coordinates)) (Max(全部x坐标)-Min(全部x坐标))*(Max(全部y坐标)-Min(全部y坐标))


  1. { (2 4), (1 1), (5 2) } {(2 4),(1 1),(5 2)}

    Minimum area for this set is equal to (5-1)*(4-1) = 4*3 = 12 该集合的最小面积等于(5-1)*(4-1)= 4 * 3 = 12

  2. { (1 1), (5 2), (17 25) } {(1 1),(5 2),(17 25)}

    Minimum area you can find for this set will be: (17-1)*(25-1) = 16*24 = 384 您可以找到的最小面积为(17-1)*(25-1)= 16 * 24 = 384

  3. { (2 4), (5 2), (17 25) } {(2 4),(5 2),(17 25)}

    Minimum area you can find for this set will be: (17-2)*(25-2) = 15*23 = 345 您可以找到的最小面积为(17-2)*(25-2)= 15 * 23 = 345

  4. { (2 4),(1 1),(17 25) } {(2 4),(1 1),(17 25)}

    Minimum area you can find for this set will be: (17-1)*(25-1) = 16*24 = 384 您可以找到的最小面积为(17-1)*(25-1)= 16 * 24 = 384


Out of all the area for the set { (2 4), (1 1), (5 2) } is minimum which is equal to 12 ,so the required answer is 12 . 在集合{(2 4),(1 1),(5 2)}的所有区域中, 最小值等于12 ,因此所需答案为12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM