Problem Statement
You are building a house and are about to lay the floorboards in a particular room. Your architect has designed the layout of the floorboards and you now want to know how many boards you need to buy. Each board is 1 unit wide and can be of any positive integer length. The room is rectangular and the boards are to be laid out on a unit-square grid inside the room, parallel to the walls of the room.
You are given a String[] layout describing the layout of the floorboards. Character j of element i of layout describes the grid-square at position (i, j) and will either be a '-' or a '|', depending on which direction the floorboard covering that square is oriented. If two '-' characters are adjacent at the same value of i, then they form part of the same east/west-oriented floorboard. Similarly, if two '|' characters are adjacent at the same value of j, they are part of the same north/south-oriented floorboard. Return an int containing the number of distinct floorboards in the layout.
Definition
- Class:
- FloorLayout
- Method:
- countBoards
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int countBoards(String[] layout)
- (be sure your method is public)
Constraints
- layout will contain between 1 and 50 elements, inclusive.
- Each element of layout will contain between 1 and 50 characters, inclusive.
- Each element of layout will contain the same number of characters.
- Each character in layout will be a '-' or a '|'.
Examples
{"----" ,"----" ,"----" ,"----"}Returns: 4
This layout contains 4 boards laid east/west.{"-||--||--" ,"--||--||-" ,"|--||--||" ,"||--||--|" ,"-||--||--" ,"--||--||-"}Returns: 31
This is an aesthetic pattern made up of boards of lengths 1 and 2.{"--------" ,"|------|" ,"||----||" ,"|||--|||" ,"||----||" ,"|------|" ,"--------"}Returns: 13
{"||-||-|||-" ,"||--||||||" ,"-|-|||||||" ,"-|-||-||-|" ,"||--|-||||" ,"||||||-||-" ,"|-||||||||" ,"||||||||||" ,"||---|--||" ,"-||-||||||"}Returns: 41
{"-||--|" ,"||||||" ,"|||-|-" ,"-||||-" ,"||||-|" ,"||-||-"}Returns: 19