[英]Java 2D Minimization
我需要編寫一個以(X,Y)形式指定給定坐標點數的程序的幫助。 給出的點數是程序的第一行; 可以通過掃描儀讀取它。
我需要計算最少的面積以覆蓋線x = a和y = b的所有點。 因此,面積將為* * b(矩形的面積)。
但是,必須刪除一個坐標點(X,Y)以優化區域。 刪除的點應盡可能減小面積。 我需要編寫算法的幫助。
這是一個示例輸入,給出的輸出是::
樣品輸入
4
2 4
1 1
5 2
17 25
樣品輸出
12
在此示例中,輸入(4)的第一行指示將輸入四個點。 接下來的四行是(x,y)形式的坐標。 最后一個點(17,25)被作為離群值移除,使我們剩下前三個點。
如果將其余三個點都繪制成圖形,則它們都可以在一個方框內(3乘4),因此輸出為12; (3 * 4)。 像本示例中的那樣,將直線對准點就可以了。 但是,離群值並不總是最后一點,或者很大。 離群值可能很小,僅需要最小化面積。
-這是我到目前為止所擁有的(我知道不是很多..)-請幫助我!
這主要只是我需要幫助的算法。
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();
}
當然,蠻力解決方案是計算點的每個組合的面積並選擇最小值。
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);
}
現在,您將擁有最小的面積和已被忽略的點。
讓您有4個點(2 4),(1 1),(5 2),(17 25) 。 由於您總是可以刪除一個點來優化面積,因此,可能存在點的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)} }
一套的最小面積為:
(Max(全部x坐標)-Min(全部x坐標))*(Max(全部y坐標)-Min(全部y坐標))
{(2 4),(1 1),(5 2)}
該集合的最小面積等於(5-1)*(4-1)= 4 * 3 = 12
{(1 1),(5 2),(17 25)}
您可以找到的最小面積為(17-1)*(25-1)= 16 * 24 = 384
{(2 4),(5 2),(17 25)}
您可以找到的最小面積為(17-2)*(25-2)= 15 * 23 = 345
{(2 4),(1 1),(17 25)}
您可以找到的最小面積為(17-1)*(25-1)= 16 * 24 = 384
在集合{(2 4),(1 1),(5 2)}的所有區域中, 最小值等於12 ,因此所需答案為12 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.