将对象添加到数组而不复制嵌套数据

时间:2016-10-04 13:36:22

标签: javascript jquery arrays object duplicates

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Island_flood {
    final static int[][] DIRECTIONS = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public static class Island {
        int islandHeight, islandWidth;
        boolean[][] visited;
        Block[][] islandMap;

        public Island(BufferedReader reader) throws IOException {
            String[] islandSizes = reader.readLine().split(" ");

            this.islandHeight = Integer.parseInt(islandSizes[0]);
            this.islandWidth = Integer.parseInt(islandSizes[1]);
            this.visited = new boolean[islandHeight][islandWidth];
            this.islandMap = new Block[islandHeight][islandWidth];

            for (int i = 0; i < this.islandHeight; i++) {
                String[] blocksRow = reader.readLine().split(" ");
                for (int j = 0; j< this.islandWidth; j++) {
                    this.islandMap[i][j] = new Block(Integer.parseInt(blocksRow[j]));
                    this.islandMap[i][j].isBorder = i == 0 || j == 0 || i == islandHeight - 1 || j == islandWidth - 1;
                }
            }
        }

        public int getTotalWaterLevel() {
            int result = 0;

            for (int i = 1; i < islandHeight - 1; i++) {
                for (int j = 1; j < islandWidth - 1; j++) {
                    bfs(new int[]{i, j});
                    int floodedValue = getFloodedValue(islandMap[i][j]);
                    int waterLevel = floodedValue - islandMap[i][j].value;
                    islandMap[i][j].value = floodedValue;
                    result += waterLevel;

                    visited = new boolean[islandHeight][islandWidth];

                    for (Block[] blockRow : islandMap) {
                        for (Block block : blockRow) {
                            block.children = new ArrayList<>();
                        }
                    }
                }
            }

            return result;
        }

        public void bfs(int[] s) {
            Queue<int[]> queue = new LinkedList<>();
            queue.offer(s);

            visited[s[0]][s[1]] = true;

            while (!queue.isEmpty()) {
                int[] p = queue.poll();
                Block b = islandMap[p[0]][p[1]];

                if (!b.isBorder) {
                    for (int[] DIRECTION : DIRECTIONS) {
                        int xPos = p[0] + DIRECTION[0];
                        int yPos = p[1] + DIRECTION[1];

                        if (!visited[xPos][yPos]) {
                            visited[xPos][yPos] = !visited[xPos][yPos];
                            b.children.add(islandMap[xPos][yPos]);
                            queue.offer(new int[]{xPos, yPos});
                        }
                    }
                }
            }
        }

        public static int getFloodedValue(Block block) {
            int result = 0;

            if (block.isBorder) {
                result = block.value;
            } else {
                ArrayList<Integer> childrenValues = new ArrayList<>();

                if (block.children.size() > 0) {
                    for (int i = 0; i < block.children.size(); i++) {
                            childrenValues.add(getFloodedValue(block.children.get(i)));
                    }

                    int minChildrenValue = Collections.min(childrenValues);

                    result = minChildrenValue > block.value ? minChildrenValue : block.value;
                }
            }

            return result;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int islandCount = Integer.parseInt(reader.readLine());
        ArrayList<Island> islands = new ArrayList<>();

        for (int i = 0; i < islandCount; i++) {
            Island island = new Island(reader);
            islands.add(island);
        }

        for (Island island : islands) System.out.println(island.getTotalWaterLevel());

        reader.close();
    }

    public static class Block {
        public boolean isBorder;
        public int value;
        public ArrayList<Block> children = new ArrayList<>();

        public Block(int value) {
            this.value = value;
        }
    }
}

确定在向数组添加对象时避免重复的最佳方法:

数据集和要求:

  • 包含用于过滤的输入字段(treeChild)的下拉列表(treeParent)列表

  • 需要将对象添加到数组mapSettings WITHOUT

    1. 复制使用相同“父”名称添加的对象
    2. 复制数组'children
    3. 中的项目

0 个答案:

没有答案