Statistics

Problem Statement for "PipePuzzle"

Problem Statement

The pipe game is played on a grid with three kinds of pipes. The straight pipe '-' allows water to flow straight. The elbow pipe 'L' diverts water either to the left or to the right. The cross pipe '+' has the same effect as a straight pipe. Unlike the other two pipes, the cross pipe allows water to flow through it a second time in a direction perpendicular to the first pass. The water source is represented by one of: 'N', 'S', 'W' or 'E' indicating whether the water will begin flowing north, south, west or east respectively. You are allowed to rotate each pipe in the grid by 90 degree increments with the objective of maximizing the number of pipes connected to the water source.

Given a String[] pipes that represents the game grid, determine the length of the longest possible flow of water. Each pipe that the water flows through adds one to the total length. A cross pipe through which water passes twice contributes two to the overall length. The starting water source is not a pipe, does not count towards the length, and may not be rotated.


{"LL-L-",
 "L+L+L",
 "--NL-",
 "L+--L",
 "LL+L-"}


This is a graphical representation of the puzzle above.

Definition

Class:
PipePuzzle
Method:
longest
Parameters:
String[]
Returns:
int
Method signature:
int longest(String[] pipes)
(be sure your method is public)

Notes

  • In each grid, south is the direction of increasing index within pipes, and north that of decreasing index. East is the direction of increasing index within an element of pipes, and west that of decreasing index.

Constraints

  • pipes will have between 1 and 20 elements, inclusive.
  • Each element of pipes will have length between 1 and 20, inclusive.
  • Every element of pipes will have the same length.
  • pipes will only contain the characters ('-', 'L', '+', 'N', 'S', 'E', 'W').
  • pipes will have exactly one water source.
  • pipes will contain between 0 and 20 elbow 'L' pipes, inclusive.

Examples

  1. {"LL-L-", "L+L+L", "--NL-", "L+--L", "LL+L-"}

    Returns: 19

  2. {"ELLL", "LLLL", "LLLL", "LLLL"}

    Returns: 13

  3. {"ELLLLL+", "++++++L", "L+++++L", "L+++++L", "L+++++L", "L+++++L", "+LLLLLL"}

    Returns: 71

  4. {"LLLLL", "LLELL", "LLLLL", "LLLLL"}

    Returns: 11

  5. {"-+-+-+-+-+-+-+-+-+-W"}

    Returns: 19

  6. {"N"}

    Returns: 0

  7. {"S"}

    Returns: 0

  8. {"E"}

    Returns: 0

  9. {"W"}

    Returns: 0

  10. {"LL", "LN"}

    Returns: 3

  11. {"L++L++L","--+N-+L","+++L+++","-++++LL","+L+L-+L","+++LL+L","+L+++++","++L+LL-","LL--L++"}

    Returns: 20

  12. {"++-LL+-","++L++L+","+LL++-L","+-+L++-","-L+L+++","--LN+L+","+LLL+LL","L+++L++"}

    Returns: 15

  13. {"LEL+L+-L-L-+","-+++--+++--+","++++L+--+++-","-L+--+-+-+-L","-L+L++++-+LL","+-LL+--LLL-+"}

    Returns: 24

  14. {"-++++++++++","++L-L++-+LL","+++++++++--","++-------L+","LL+++L-++++","-++L+-+-+++","++N+L-L-+L+","+L++++++L-L"}

    Returns: 20

  15. {"++LLLL-LL+++-+++","--+-+L+-++-LL-+-","--++----++L-++L-","L++-L++++---+-+L","L++--+--+++--+++","+L+L+-+W+L++L-+L"}

    Returns: 36

  16. {"++++++","+L++++","++-+LL","L+L+++","++L+LS","++++LL","L+++++"}

    Returns: 16

  17. {"+++++-+-++++","-+-++---+-++","SLL++-++++-+","-++-+-L-L-++","-+L++++L++++","--++--+++LL+","++++++--+++-","LL--L++L++++","-L++++-+++++","---+L+L-++++","-++--+++-+++","++L+++L+++++","++++++++++++","+++++++-++++","+-++++++L++-","++-+++++++++"}

    Returns: 56

  18. {"+LL","L+L","-LL","LLL","S++","L+L","+++","+LL","LL+","++L","+LL","+L+"}

    Returns: 21

  19. {"+++++++","+++L+L+","-+-+++L","++++L++","++++L+S","++L+L++","++L++L+","++L+L++","+++++++","+++++LL","+++++++","+LL+L++","+LLL+++"}

    Returns: 40

  20. {"++LL","LL-L","-L+W","+-L+","LLLL","L-+L","+-LL","+LL-"}

    Returns: 16

  21. {"LL+L","LL+L","LLLL","SLL+","L+LL"}

    Returns: 14

  22. {"+++++-+-+++--+-+++-L","+++++--+L+++++++++++","++++++++++++++++-+++","+---+-+++++-++++++++","+++-++++-+++++++++++","++++-+++++-++++-+-++","-+++++--+++-L+++++++","+++++++-+++++++++--+","--+++-+-+-+++-++++LL","++++++++++++L++++++-","+-++++--+++++-+-+-++","L+++++-+++++++++--++","++++-++++-++++++++++","++L+--+-+-+-+-----++","++++--+-++L+++++++++","++-+++++L+++++++++++","+++++++++++-+-++++-+","+-+++++++++-+-++++-+","++++L+++-+++++-+-+LN","++++-++-+++++--+-+++"}

    Returns: 53

  23. {"+LL","LL+","+++","L+L","LLL","LNL"}

    Returns: 12

  24. {"-+-+++L","LL+LLLL","-L-+---","+L+--++","+LL-+-L","+--L+NL","+LL+LL-"}

    Returns: 18

  25. {"LL+WLLLL","LLLL++L+","LLLLL+-L"}

    Returns: 22

  26. {"L++-+-+L+","LL+LE-+LL","+++-++-L+","+++---+++","+++-++-++","L------++","+-+--+-++","++++L--+-","-+L-L++++","--+++++++"}

    Returns: 21

  27. {"-+++L+++++L+L++++","-++++-++++-+++++L","++++++L++L+++L++L","+++++L+++++-+--+L","+--+++--+++++++L+","+-++++++++++L++++","-+-E+-++++++L+++-","L++-++++++++L+-L+"}

    Returns: 31

  28. {"L++LL+","++L+++","+++LL-","L++++L","++++++","++++++","-++L+L","L-++L+","+--+++","LL+--+","-+L+++","L+S--+","++++++","+LL+++"}

    Returns: 45

  29. {"--+L+L+++++","-+S++-++--+","-+-L-+---+-","+-++-+-L---","-+L+++++-+L","+---L+--+L+","+++L-+-+++-","--+++++-+++","++-+-----L+","-L+---+++LL","---+-+++-+-","--++--+-L++","++---+++++L","-+++-++++L-"}

    Returns: 30

  30. {"+L++LL+L++LL","+L+L-+++-++L","+LLL++--L+LN","L+-+L-+-+L+L"}

    Returns: 16

  31. {"+-+L+L","--L-++","++LLL+","L++++-","L+L+W+","++L--L","--++++","+-+-LL","+++L+-"}

    Returns: 20

  32. {"+++++L++++++++L+","++++++++++++++++","+LL++L+W++++++++","++L++L++++++++++","++++++++++++++++","++-+++++++L-++++","+++++++-++L+++++","++++++++++++L+L+","+++++++LL+++++++","L+L-++++++++++++","L+++-++L++++++++","++++--++++-+++++","++++-+++++++++++","++++++++++++++++","++++++++++++++++","++++++++++++++++","-+++++++++++++++","+++++L++++++++++","+++++++L+++++-++"}

    Returns: 38

  33. {"L++L","LL-L","+-+L","-LL+","LLN+","LL--","L++L","L+-L","L-+-"}

    Returns: 23

  34. {"++LL++L++","+++LLL+++","-+L++++++","+++-++L++","L+L+-+WL+","-+++++L++","L+L+++L++","++L++++L+"}

    Returns: 17

  35. {"--L-+L+---L","L+++-LLL--+","+++L-L+++LL","+-+LLL++LL+","L+++-WL+++-"}

    Returns: 26

  36. {"+L++","LL+L","LE+L","LLLL"}

    Returns: 8

  37. {"L+L+++","++L+LL","LLLL-L","L++L-+","+L-LL+","LLNLL+"}

    Returns: 13

  38. {"L-+L+L-+--++","L-++L--+++-+","+++++--++L++","+-+++++++--+","--++LL-+-+-+","+E-L++-+++-+","+++++---++++","++-+--++-+++","+++-L-++L+++","++++--+LL+L+","-+---++L+L-+"}

    Returns: 38

  39. {"+LWL","++L+","LL++","+-+-","+L++","+LL+","+L+L","L++L","+L+L","+LL+"}

    Returns: 31

  40. {"+--+L++-L--++-+-+LL","+-++-+-++-LL+E+L+-+","--+L+-LL+++-++L+++L","--++L+++++++---L++-","-++---+-+-++L--L---"}

    Returns: 26

  41. {"-++++++","L+-+--+","++++++-","-++-+++","-+++-++","-+-+++S","+++L+++","+++LLLL","++-++-L","++-+++L","+++-+++","++L+L+-","+L-+-L+","++L++++"}

    Returns: 24

  42. {"+++++L+","++L++L+","+++++++","++NL+++","L++++-L","++++-++","+-+++++","+--+++L","++++++L","+++L+++","++-+++L","++L+++L","+++L++-","++-++++","+++++L-"}

    Returns: 23

  43. {"+LL++L-++-+++L+","+++++++-++-++++","+L++-+-+++L++++","++++++-+++-L++-","+-+++-++++-++++","++++++++++++++-","++++++++++++--+","+-+++++++++++++","+LL+++++++++-++","++++++-+++L-++L","++++++L+L-E+++L","+-+++++++++L+LL","-++++L++++++++-","+-++++-+-+++++L"}

    Returns: 64

  44. {"L+LL+","L+++L","L++L+","LLN+L","LL++L"}

    Returns: 17

  45. {"++-+L+++++-+LL--++","-++++---++-+-++-++","+++S++--+++--+-++-","-+L++-+-++--L---L-","L+LL-++-+-+L+++L++","-++++-++++++-+-+--","+-+--+-++++-+++-++","+++++++--+++++-+++","+-+-++--++L++-L-++","++L++L+L-+++---+++","+-+--+--+---+-+-+-"}

    Returns: 35

  46. {"-L-L+","+L+LW","+-++L","-+LL+","L-LL-","L+-LL"}

    Returns: 15

  47. {"+++++++++++++++++","++L++++++++++++++","+++++++++++++++S+","++++++++++++++-++","+++++++++++++++++","++++++++++++++++L","+++++-L++++++++++","+-++L+L++++++++++","L++++++++++++L+++","+L+++++-++++++-++","+++++++++++++++++","+++++L++++L++++++","++++++++++++L++++","+++++++++-+++++LL","++LL+++++L++++++L"}

    Returns: 36

  48. {"+++++++LL+-","+++-+L+LL-L","LL--++++L++","+L-L++L+++-","LL+++-+--++","+++++++E++L","+-+L+L+++L+"}

    Returns: 40

  49. {"+++++++++L+++++","+++++L++L+L++L+","+++L+++L-++++L+","+++LL+L++++++++","+++L+++++LL+++L","+-+++LL+++E+++L"}

    Returns: 25

  50. {"LLL+EL","LL+LLL","L+LLLL","LLLLL+"}

    Returns: 17

  51. { "EL", "LL" }

    Returns: 3

  52. { "L------------L------", "N++++++++++++L++++++" }

    Returns: 27

  53. { "L------------L------", "E++++++++++++L++++++" }

    Returns: 27

  54. { "L------------L------", "N++++++++L+++L++++++" }

    Returns: 21

  55. { "L------------L------", "E++++++++L+++L++++++" }

    Returns: 10

  56. { "+--+-+-+-++-++-+-+-+", "+--+-+-+-++-++-+-+-+", "+--+N+-+-++-++-+-+-+", "-------+-+++++++++++", "++++++-+-++-++------", "+-+-++-++-+-+-++--+-" }

    Returns: 2

  57. { "+--+-+-+-+++-+-+-+-+", "+--+-+-+-+++-+-+-+-+", "+--+-+-+-+++-+-+-+-+", "-E-----+++++++++++++", "++++++-+-+++-+------", "+-+-++-++-+-+-++--+-" }

    Returns: 18

  58. { "+--+-+-++-++-+-+-+-+", "+--+-+-++-++-+-+-+-+", "+--+++-++-++-+-+-+-+", "-------+++++++++++++", "+++W++-++-++-++-----", "+-+-++--+-+-+-++--+-" }

    Returns: 3

  59. { "+--+-+-++-++-S-+-+-+", "+--+-+-++-++-+-+-+-+", "+--+++-++-++-+-+-+-+", "-------+++++++++++++", "++++++-++-++-+------", "+-+-++--+-+-+-++--+-" }

    Returns: 5

  60. {"ELLLLL+", "++++++L", "L+++++L", "L+++++L", "L+++++L", "L+++++L", "+LLLLLL" }

    Returns: 71


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: