使用数组作为地图JAVA

时间:2017-01-23 14:16:32

标签: java arrays dictionary

我正在尝试创建一个程序,使用公式d =((x 2 - x 1)给出放置医院的最佳位置 2 +(y 2 - y 1 2 0.5 。最佳位置位于绿色圆圈中,坐标为(11,8),(9,9),(9,19)和(13,13)。

image of maps

这是我到目前为止的代码:

public class hospitalBuilderProgram
{
    public static void main(String[] args)
    {
        byte[][][] maps = {
            {{2, 2}, {2, 8}, {5, 15}, {19, 1}, {17, 17}},
            {{1, 1}, {7, 19}, {13, 19}, {19, 7}, {19, 13}},
            {{0, 19}, {2, 19}, {4, 19}, {6, 19}, {18, 19}},
            {{7, 19}, {13, 19}, {19, 19}, {19, 13}, {19, 7}}
        };

        for (int row = 1; row <= 19; row++) {
            for (int col = 1; col <= 19; col++) {
                int d1 = (((x1 - row) ^ 2 + (y1 - col) ^ 2));
                int d2 = ((x2 - row) ^ 2 + (y2 - col) ^ 2);
                int d3 = ((x3 - row) ^ 2 + (y3 - col) ^ 2);
                int d4 = ((x4 - row) ^ 2 + (y4 - col) ^ 2);
                int d5 = ((x5 - row) ^ 2 + (y5 - col) ^ 2);

                if (d1 == d2 == d3 == d4 == d5)
                    return (row, col);
            }
        }
    }
}

我正在尝试相应地修改我的代码,但我不确定如何继续。

1 个答案:

答案 0 :(得分:0)

这个解决方案阐述了不同位置的重心:

public static void locateHospital() {
   final byte[][][] maps = {
      {{2,  2}, { 2,  8}, { 5, 15}, {19,  1}, {17, 17}},
      {{1,  1}, { 7, 19}, {13, 19}, {19,  7}, {19, 13}},
      {{0, 19}, { 2, 19}, { 4, 19}, { 6, 19}, {18, 19}},
      {{7, 19}, {13, 19}, {19, 19}, {19, 13}, {19,  7}}
   };
   for( final byte[][] map : maps ) {
      int x = 0;
      int y = 0;
      for( final byte[] coords : map ) {
         x += coords[0];
         y += coords[1];
      }
      x /= 5;
      y /= 5;
      System.out.printf( "Hospital should be placed in {%d, %d}\n", x, y );
   }
}

输出:

Hospital should be placed in {9, 8}
Hospital should be placed in {11, 11}
Hospital should be placed in {6, 19}
Hospital should be placed in {15, 15}

这不是你的问题,也不是你的解决方案,但它可以帮助...; - )

为了更准确,数据描述的每个位置都应该让其人口对重心进行加权。