Menu
Question Index
...


Write a program to fit the blocks such that it forms a perfect rectangular box with out any gaps. The blocks are further comprised of cells and they are of irregular shape. In the integer array defining the block, 0 indicates there is no cell and 1 indicates cell. The blocks given might be any direction and they might have to be turned to fit into a proper block. Also note that the answers given here are indicative and any other solution will also be accepted as long as it is perfect rectangular box with out any gaps.

Input (List) Output (char[][])
{[A, {{1, 1, 0}, {1, 1, 1}, {1, 1, 1}, {1, 1, 0}, {1, 1, 0}}], [B, {{1, 1, 0, 0}, {0, 1, 1, 1}}], [C, {{1, 1, 1}, {0, 0, 1}}], [D, {{0, 1, 1}, {1, 1, 1}}], [E, {{1, 0}, {1, 1}}], [F, {{0, 1, 1}, {0, 1, 1}, {1, 1, 1}}], [G, {{1, 1, 0, 0}, {1, 0, 0, 0}, {1, 1, 1, 1}}], [H, {{1, 1}, {1, 1}, {1, 1}, {1, 1}}], [I, {{1}, {1}, {1}, {1}}]}
A A B B C C C G G D D
A A A B B B C G D D D
A A A E F F F G G G G
A A E E F F F H H H H
A A I I I I F H H H H
{[A, {{1, 0, 0}, {1, 1, 1}, {0, 0, 1}}], [B, {{1, 1, 1, 1}, {0, 0, 1, 0}, {0, 0, 1, 0}}], [C, {{0, 1, 1}, {1, 1, 1}, {1, 0, 1}}], [D, {{1}}], [E, {{1, 1}}] } A B B B B C C
A A A B C C C
E E A B C D C
{[A, {{1, 1, 1, 1, 1}}], [B, {{1, 1, 1, 1, 1}}], [C, {{1, 1, 1, 1, 1}}]} A A A A A B B B B B C C C C C
{[A, {{1, 1, 1}}], [B, {{1, 1, 1}, {0, 1, 0}}], [C, {{1, 0}, {1, 1}}], [D, {{1, 1}}], [E, {{1, 1}, {1, 1}}]} C C B A A A E E
C B B B D D E E
{[A, {{1, 1}, {0, 1}}], [B, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}]} A A B
B A B
B B B

class FitBlocks

{    public static void main(String s[])
    {
        int[][] aPositions = {{1, 1, 0}, {1, 1, 1}, {1, 1, 1}, {1, 1, 0}, {1, 1, 0}};
        Block aBlock = new Block('A', aPositions);
        int[][] bPositions = {{1, 1, 0, 0}, {0, 1, 1, 1}};
        Block bBlock = new Block('B', bPositions);
        int[][] cPositions = {{1, 1, 1}, {0, 0, 1}};
        Block cBlock = new Block('C', cPositions);
        int[][] dPositions = {{0, 1, 1}, {1, 1, 1}};
        Block dBlock = new Block('D', dPositions);
        int[][] ePositions = {{1, 0}, {1, 1}};
        Block eBlock = new Block('E', ePositions);
        int[][] fPositions = {{0, 1, 1}, {0, 1, 1}, {1, 1, 1}};
        Block fBlock = new Block('F', fPositions);
        int[][] gPositions = {{1, 1, 0, 0}, {1, 0, 0, 0}, {1, 1, 1, 1}};
        Block gBlock = new Block('G', gPositions);
        int[][] hPositions = {{1, 1}, {1, 1}, {1, 1}, {1, 1}};
        Block hBlock = new Block('H', hPositions);
        int[][] iPositions = {{1}, {1}, {1}, {1}};
        Block iBlock = new Block('I', iPositions);
        List blocks = new ArrayList();
        blocks.add(aBlock);
        blocks.add(bBlock);
        blocks.add(cBlock);
        blocks.add(dBlock);
        blocks.add(eBlock);
        blocks.add(fBlock);
        blocks.add(gBlock);
        blocks.add(hBlock);
        blocks.add(iBlock);
        char[][] result = fitBlocks(blocks);
        System.out.println("The rectangular box after the arrangement is : \n");
        if (result != null) {
        for (char[] row : result) {
        for (char element : row) {
        System.out.print(element + " ");
        }
        System.out.println(" ");
        }
        }

    }


public static char[][] fitBlocks(List<Block> blocks) {
//Write code here to fit the blocks into a perfect rectangular box with no gaps
}

//If required, write any additional methods here
}
enum Direction {
South, East, West, North;
}
class Block {

char code;
int[][] positions;

public Block(char code, int[][] positions) {
this.code = code;
this.positions = positions;
}

public char getCode() {
return code;
}

public int[][] getPositions() {
return positions;
}

//If required, add any additional member variables or methods here
}

Doubts

Problems

Topic: Java EnumMap

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App