2016-11-11 29 views
0

我使用地形表示法制作柵欄,其中1是圍欄土地,0是空的空間。這裏將是例如地圖:使用地形表示法的柵欄的中間部分

代碼是如何工作的是它會掃描角和1的側面和計數多少您需要的米數的柵欄,其中1的每邊爲2.5米。 j變量就像x值,而i是y值。我有邊和邊角的工作代碼,我需要幫助的唯一的事情是我如何去設置for循環的中心柵欄。這是我的工作代碼:

package workspace; 

public class Fencing { 

public static void main(String[] args) { 
    boolean b = true; 
    double corner = 0; 
    double center = 0; 
    double side = 0; 
    int i = 0; 
    int j = 0; 
    int[][] map = { 
     { 0, 1, 1, 0 }, 
     { 1, 1, 1, 1 }, 
     { 1, 1, 1, 1 }, 
     { 1, 1, 0, 1 } 
    }; 
    for (i = 0; i < 3; i++) { 
     for (j = 0; j < 3; j++) { 
      if (map[i][j] != 1 && map[i][j] != 0) { 
       b = false; 
       System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j); 
      } 
     } 
    } 
    if (b == false && i == 3) { 
     System.out.println("The map is invalid"); 
     return; 
    } else { 
     System.out.println("Map is valid"); 
    } 
    if (map[0][0] == 1) { 
     corner += 5; 
     if (map[1][0] == 1 && map[0][1] == 1) { 
      corner += 0; 
     } 
     if (map[1][0] != 1 && map[0][1] != 1) { 
      corner += 5; 
     } 
     if (map[1][0] != 1 && map[0][1] == 1 || map[1][0] == 1 && map[0][1] != 1) { 
      corner += 2.5; 
     } 
    } else { 
     corner += 0; 
    } 
    if (map[0][3] == 1) { 
     corner += 5; 
     if (map[1][3] == 1 && map[0][2] == 1) { 
      corner += 0; 
     } 
     if (map[1][3] != 1 && map[0][2] != 1) { 
      corner += 5; 
     } 
     if (map[1][3] != 1 && map[0][2] == 1 || map[1][3] == 1 && map[0][2] != 1) { 
      corner += 2.5; 
     } 
    } else { 
     corner += 0; 
    } 
    if (map[3][0] == 1) { 
     corner += 5; 
     if (map[3][1] == 1 && map[2][0] == 1) { 
      corner += 0; 
     } 
     if (map[3][1] != 1 && map[02][0] != 1) { 
      corner += 5; 
     } 
     if (map[3][1] != 1 && map[2][0] == 1 || map[3][1] == 1 && map[2][0] != 1) { 
      corner += 2.5; 
     } 
    } else { 
     corner += 0; 
    } 
    if (map[3][3] == 1) { 
     corner += 5; 
     if (map[3][2] == 1 && map[2][3] == 1) { 
      corner += 0; 
     } 
     if (map[3][2] != 1 && map[2][3] != 1) { 
      corner += 5; 
     } 
     if (map[3][2] != 1 && map[2][3] == 1 || map[3][2] == 1 && map[2][3] != 1) { 
      corner += 2.5; 
     } 
    } else { 
     corner += 0; 
    } 
    System.out.println("Corner fencing = " + corner); 

    i = 0; 
    j = 0; 
    for (j = 1; j < 3; j++) { // Top 
     if (map[0][j] == 1) { 
      side += 2.5; 
      if (map[0][j + 1] == 1 && map[0][j - 1] == 1) { 
       side += 0; 
      } 
      if (map[0][j + 1] != 1 || map[0][j - 1] != 1) { 
       side += 2.5; 
      } 
      if (map[0][j + 1] != 1 && map[0][j - 1] != 1) { 
       side += 5; 
      } 
     } else { 
      side += 0; 
     } 
    } 
    System.out.println("After the first row, fencing = " + side); 

    i = 0; 
    j = 0; 
    for (j = 1; j < 3; j++) { // Bottom 
     if (map[3][j] == 1) { 
      side += 2.5; 
      if (map[3][j + 1] == 1 && map[3][j - 1] == 1) { 
       side += 0; 
      } 
      if (map[3][j + 1] != 1 || map[3][j - 1] != 1) { 
       side += 2.5; 
      } 
      if (map[3][j + 1] != 1 && map[3][j - 1] != 1) { 
       side += 5; 
      } 
     } else { 
      side += 0; 
     } 
    } 
    System.out.println("After the bottom row, fencing = " + side); 

    i = 0; 
    j = 0; 
    for (i = 1; i < 3; i++) {// Right 
     if (map[i][3] == 1) { 
      side += 2.5; 
      if (map[i + 1][3] == 1 && map[i - 1][3] == 1) { 
       side += 0; 
      } 
      if (map[i + 1][3] != 1 || map[i - 1][3] != 1) { 
       side += 2.5; 
      } 
      if (map[i + 1][3] != 1 && map[i - 1][3] != 1) { 
       side += 5; 
      } 
     } else { 
      side += 0; 
     } 
    } 
    System.out.println("After the first column, fencing = " + side); 

    i = 0; 
    j = 0; 
    for (i = 1; i < 3; i++) {// Left 
     if (map[i][0] == 1) { 
      side += 2.5; 
      if (map[i + 1][0] == 1 && map[i - 1][0] == 1) { 
       side += 0; 
      } 
      if (map[i + 1][0] != 1 || map[i - 1][0] != 1) { 
       side += 2.5; 
      } 
      if (map[i + 1][0] != 1 && map[i - 1][0] != 1) { 
       side += 5; 
      } 
     } else { 
      side += 0; 
     } 
    } 
    System.out.println("After the last column, fencing = " + side); 

} 
} 

我不確定如果我甚至會用一個for循環掃描的核心,但我想它會是這樣的一側循環。任何人都有關於代碼的樣子的想法?

+0

該示例的期望輸出是什麼? –

+0

對於我提供的某個映射(與陣列中的映射相同),它將爲2.5,因爲中心只有一個1位於旁邊0 –

+1

如果您在codereview.stackexchange處提問此問題.com,他們會提出一個更清晰的寫作方式,使它更短,更易於理解。你有很多複製粘貼的事情,每當你複製粘貼代碼時,都有更好的方式來做事。 – tucuxi

回答

0

因此,給定一個零和一個矩陣,你想要計算從一到零(上 - 下,左 - 右)你能找到多少個轉換。這些都將導致2.5米長的圍欄。邊界計爲零。

你有很多複製粘貼的if語句來檢查這個。我建議下面的重寫,它使用方法來避免複製粘貼代碼。

/** 
* returns the number of zero-or-border neighbors for a 1 
* in a 2D array of ints that can be either 1 or 0 
*/ 
public static int bordersFor(int[][] map, int row, int col) { 
    int borders = 0; 
    // left 
    if (col == 0 || map[row][col-1] == 0) borders ++; 
    // right 
    if (col == map[0].length-1 || map[row][col+1] == 0) borders ++; 
    // up 
    if (row == 0 || map[row-1][col] == 0) borders ++; 
    // down 
    if (row == map.length-1 || map[row+1][col] == 0) borders ++; 

    return borders; 
} 

public static int bordersFor(int[][] map) { 
    int borders = 0; 
    for (int row=0; row<map.length; row++) { 
     for (int col=0; col<map[0].length; col++) { 
      if (map[row][col] == 1) borders += bordersFor(map, row, col); 
     } 
    } 
    return borders; 
} 

public static void main(String ... args) { 
    int[][] map = { 
      { 0, 1, 1, 0 }, 
      { 1, 1, 1, 1 }, 
      { 1, 1, 1, 1 }, 
      { 1, 1, 0, 1 } 
    }; 
    System.out.println(bordersFor(map)); 
} 
相關問題