GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

skyscraper puzzle

Description of the puzzle

• For a NxN grid, fill in the grid with digits 1 – N so that each row and column contains each digit exactly once

• Each number inside the grid represents the height of a building

• The clues outside of the grid indicate how many buildings can be seen when looking from that direction

• Taller buildings block the view of smaller buildings

• If no clue is given, then it could mean that any valid number is possible for that clue

Winning Criteria: All rows and columns should be filled with unique numbers in any order from 1 to n indicating the number of floors.

All the clues of the board must be satisfied. Each clue tell us how many buildings can be seen from that direction. If clue is N, then all buildings must be visible from that direction so you will insert numbers from 1 to N such that no building is hidden.If clue is 1 then you will place the largest number i.e. N at at first so that all smaller floor buildings are hidden behind.

Further details with figures can be found here: http://www.conceptispuzzles.com/index.aspx?uri=puzzle/skyscrapers/techniques

Description of the puzzle

• For a NxN grid, fill in the grid with digits 1 – N so that each row and column contains each digit exactly once

• Each number inside the grid represents the height of a building

• The clues outside of the grid indicate how many buildings can be seen when looking from that direction

• Taller buildings block the view of smaller buildings

• If no clue is given, then it could mean that any valid number is possible for that clue

Puzzle example:

enter image description here

In above example, column 0 has a clue 5 that means only 5 buildings should be visible in this column from that direction. So in column 0, we can place the buildings like this [2,3,4,5,6,1]. By this only 5 buildings will be visible 2 to 6 and building with 1 floor is hidden by building with 6 floors or other possible way will be 2,3,4,1,5,6. so by this we will have visible buildings with floors 2,3,4,5,6. Building with 1 floor is hidden as its behind the building of 4 floors.

Solution:

enter image description here

Format for the puzzle input file

Filestructure:

N

Comma separated visibility count for each column from the top (from left to right) (X denotes blank input)

Comma separated visibility count for each column from the bottom (from left to right) (X denotes blank input)

Comma separated visibility count for each row from the left (from top to bottom) (X denotes blank input)

Comma separated visibility count for each row from the right (from top to bottom) (X denotes blank input)

For the above puzzle, the input will be:

6

5,X,X,2,2,X

X,3,4,X,X,4

X,2,3,4,X,X

X,X,4,3,2,X

*Format for the output *

Comma separated values for each row (from left to right) One line for each row (rows should be printed from top to bottom)

For the solution to the above puzzle, the output should be:

2,1,3,4,5,6

3,6,4,2,1,5

4,5,6,3,2,1

1,2,5,6,4,3

5,3,2,1,6,4

6,4,1,5,3,2

Sample Input

6

5,X,X,2,2,X

X,3,4,X,X,4

X,2,3,4,X,X

X,X,4,3,2,X

Sample Output

2,1,3,4,5,6

3,6,4,2,1,5

4,5,6,3,2,1

1,2,5,6,4,3

5,3,2,1,6,4

6,4,1,5,3,2

asked Jul 11, 2016 by anonymous
...