Connection Status:
Competition Arena > Islands
SRM 477 · 2009-11-12 · by keshav_57 · Simple Math, Simple Search, Iteration
Class Name: Islands
Return Type: int
Method Name: beachLength
Arg Types: (vector<string>)
Problem Statement

Problem Statement

The king is trying to find new ways to generate revenue, and he is currently exploring tourism as one potential avenue. The kingdom is a group of islands, and the amount of revenue that can be generated depends on the combined total length of beaches on all the islands.

You are given a String[] kingdom consisting of '.' or '#' characters. '#' represents a land mass, whereas '.' represents water. kingdom[i][j] represents a regular-hexagon shaped area with each side of unit length. Since the cells are hexagonal in shape, the odd-numbered rows (0-based) are 'shifted' towards the right. A beach is a segment which has water on one side, and land on the other.
An example String[] and the corresponding image are given below to illustrate. The beaches are marked in red.
{"..#.##",
 ".##.#.",
 "#.#..."}


Return the combined total length of beaches on all the islands.

Constraints

  • kingdom will contain between 1 and 50 elements, inclusive.
  • Each element of kingdom will contain between 1 and 50 characters, inclusive.
  • Each element of kingdom will contain the same number of characters.
  • Each character in kingdom will be either '.' or '#'.
Examples
0)
{"##...##", "#.#.#..", "#..####", "..###..", "#.##..#", ".....#.", "##.....", "..#.###", "##.###.", ".##...."}
Returns: 91
1)
{".#...#.."}
Returns: 4

There are two small islands with water on two sides of each island.

2)
{"..#.##", 
 ".##.#.", 
 "#.#..."}
Returns: 19

The example in the problem statement.

3)
{"#...#.....",
 "##..#...#."}
Returns: 15
4)
{"....#.",
 ".#....",
 "..#..#",
 "####.."}
Returns: 24

Submissions are judged against all 105 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Islands with a public method int beachLength(vector<string> kingdom) · 105 test cases · 2 s / 256 MB per case

Submitting as anonymous