Statistics

Problem Statement for "SlidingDoors"

Problem Statement

We have a wardrobe. The wardrobe is divided into N equally wide sections. The sections are placed next to each other from the left to the right and numbered from 0 to N-1 in that order.

The wardrobe has sliding doors. The sliding doors consist of exactly N separate panels. Each of the panels is exactly as wide as one of the sections. We can slide the panels left and right along the rails to get to the section of the wardrobe we want.

This is complicated by the fact that there are only two rails, so usually multiple panels share the same rail. Sometimes we then need to slide away multiple panels just to get to the section we want.


You are given the String[] rails that describes the current placement of the panels. The String rails[0] represents the back rail, the String rails[1] is the front rail. For simplicity, we will call the two rails "rail 0" and "rail 1".

In each of these Strings character at index x is '-' if there is currently a panel on this rail in front of section x of the wardrobe, or '.' if there is not.

You are also given the number S. You want to access the section number S in your wardrobe.


Slide the panels along the rails in a way that will allow you to access section S once you are done.

Return a String[] in the same format as rails. The returned String[] should describe the final configuration of panels once you are done moving them.

Any valid solution will be accepted.

Definition

Class:
SlidingDoors
Method:
openSection
Parameters:
int, String[], int
Returns:
String[]
Method signature:
String[] openSection(int N, String[] rails, int S)
(be sure your method is public)

Notes

  • It is not necessary to minimize the number of times you move a panel.
  • You can only move panels left and right along the rail they are on. You cannot move panels from one rail to another.
  • Panels on the same rail can never share the same location. E.g., if you want to move a panel A to the left but there is already another panel B there, you must first move panel B to the left and then you can move panel A into the new empty space.
  • For the constraints given below a solution always exists.

Constraints

  • N will be between 2 and 100, inclusive.
  • rails will contain exactly 2 elements.
  • Each element of rails will contain exactly N characters.
  • Each character in rails will be either '-' or '.'.
  • There will be a total of exactly N '-' characters in rails.
  • Each element of rail will contain at least one '-' and at least one '.'.

Examples

  1. 2

    {"-.", ".-"}

    0

    Returns: {".-", ".-" }

    This is the smallest possible wardrobe: with two sections, numbered 0 and 1. The wardrobe is completely closed: on rail 0 we have a panel in front of section 0, and on rail 1 we have a panel in front of section 1 of the wardrobe. We want to access section 0. In order to do so, we must slide the panel on rail 0 to the right. In the final configuration there are now two panels in front of section 1 (one on rail 0, the other on rail 1) and the access to section 0 is free.

  2. 3

    {"-.-", ".-."}

    1

    Returns: {"-.-", "-.." }

    A completely closed wardrobe with N = 3 sections. In order to access section 1, we need to push the only panel on rail 1 out of the way: either to the left or to the right.

  3. 3

    {"--.", ".-."}

    1

    Returns: {"-.-", "-.." }

    As in the previous example, we want to access section 1. Now there are two panels in our way and we need to push both of them aside. As before, we have two options what to do with the panel on rail 1, but there is only one option what to do with the other panel - we must push that one to the right.

  4. 6

    {"...---", "...---"}

    2

    Returns: {"...---", "...---" }

    Each rail has three panels, and on each rail the three panels are currently pushed as far right as possible. The access to section 2 is free, so we don't have to do anything.

  5. 6

    {"...---", "...---"}

    4

    Returns: {"..--.-", "..--.-" }

    If we want to access section 4 of this wardrobe, we'll need to do quite a bit of work. One possible solution (one that uses as few moves as possible) is shown below as a step-by-step guide. Note that other solutions are possible and that you are not required to minimize the number of panel moves. start: goal: 012345 012345 012345 012345 012345 ...--- -> ..-.-- -> ..--.- -> ..--.- -> ..--.- ...--- ...--- ...--- ..-.-- ..--.- ^ | section 4 now accessible

  6. 47

    {"------------------------.......................", "-----------------------........................"}

    4

    Returns: {"----.--------------------......................", "----.-------------------......................." }

  7. 57

    {".....................-----------------------------.......", ".....................----------------------------........"}

    35

    Returns: {"-----------------------------............................", "----------------------------............................." }

  8. 60

    {".....................-------------------------------........", "......................-----------------------------........."}

    36

    Returns: {"-------------------------------.............................", "-----------------------------..............................." }

  9. 45

    {".....-----------------------.................", ".....----------------------.................."}

    16

    Returns: {"----------------.-------.....................", "----------------.------......................" }

  10. 67

    {"................................----------------------------------.", ".................................---------------------------------."}

    49

    Returns: {"----------------------------------.................................", "---------------------------------.................................." }

  11. 52

    {"..........................--------------------------", "..........................--------------------------"}

    39

    Returns: {"--------------------------..........................", "--------------------------.........................." }

  12. 7

    {"...----", "....---"}

    6

    Returns: {"----...", "---...." }

  13. 55

    {"...........................----------------------------", "............................---------------------------"}

    42

    Returns: {"----------------------------...........................", "---------------------------............................" }

  14. 5

    {"---..", "--..."}

    0

    Returns: {".---.", ".--.." }

  15. 2

    {".-", ".-"}

    1

    Returns: {"-.", "-." }

  16. 63

    {"..............................--------------------------------.", "...............................-------------------------------."}

    46

    Returns: {"--------------------------------...............................", "-------------------------------................................" }

  17. 71

    {"------------------------------------...................................", "-----------------------------------...................................."}

    1

    Returns: {"-.-----------------------------------..................................", "-.----------------------------------..................................." }

  18. 42

    {".....................---------------------", ".....................---------------------"}

    36

    Returns: {"---------------------.....................", "---------------------....................." }

  19. 56

    {"-----------------------------...........................", ".---------------------------............................"}

    14

    Returns: {"--------------.---------------..........................", "--------------.-------------............................" }

  20. 32

    {"..............-----------------.", "...............---------------.."}

    22

    Returns: {"-----------------...............", "---------------................." }

  21. 58

    {".-----------------------------............................", ".-----------------------------............................"}

    15

    Returns: {"---------------.--------------............................", "---------------.--------------............................" }

  22. 6

    {"...---", "...---"}

    4

    Returns: {"..--.-", "..--.-" }

  23. 49

    {"........................-------------------------", ".........................------------------------"}

    45

    Returns: {"-------------------------........................", "------------------------........................." }

  24. 90

    {".......................---------------------------------------------......................", ".......................---------------------------------------------......................"}

    45

    Returns: {"---------------------------------------------.............................................", "---------------------------------------------............................................." }

  25. 78

    {"........................---------------------------------------...............", "........................---------------------------------------..............."}

    43

    Returns: {"---------------------------------------.......................................", "---------------------------------------......................................." }

  26. 73

    {"...--..-.-.-------.-.-.-....-..-.-..-.....-.-.-.-....--.-..---...-....--.", "..-.-----.-.--.-.-....--.--...-.---.-.---.--.-...-.---.--.-....-------.--"}

    25

    Returns: {"-------------------------.------.........................................", "-------------------------.-----------------.............................." }

  27. 78

    {"..---...-.---.-..-..---......--.-..----.---.--.-.------..-....-.-----.---..---", "-.-.---.--..--...-......-....-..-.-....-.-..-.--...---.--...-.-----...-...-.--"}

    50

    Returns: {"--------------------------------------------..................................", "----------------------------------............................................" }

  28. 46

    {"--..--..----.--.-.--...-..--.-...--..-.-..-..-", "...-...-.-......-..-.------..-.-..---------..-"}

    2

    Returns: {"--..--..----.--.-.--...-..--.-...--..-.-..-..-", "...-...-.-......-..-.------..-.-..---------..-" }

  29. 83

    {"....--.-.---.-.----.-...--.--.--.-.-.-.---....-.-..--....--...--.-..----.-.-.---.--", ".-.-..-..-....--.-.....---.--......-----.---...--......--.--...---.----.--.-.--.-.."}

    69

    Returns: {"--------------------------------------------.......................................", "---------------------------------------............................................" }

  30. 100

    {"-.-----....-.....-..-.-..----.-----.----.---.....---..--...-..-....-..-...--.----...-.--.-..---..--.", ".-.--.-.....-..-.---..-----..-.---.-.-.-.-..---..-.....-..--.-.-.--.---.-..--.----.-..-.-..-.-...--."}

    65

    Returns: {"--------------------------------------------------..................................................", "--------------------------------------------------.................................................." }

  31. 91

    {"..-.--.---..--.-.-...----.--.-..--.-.-.-..--.-.-.-...--......--.-----.--...---.-....-.----.", "---..--....--..----.-.-.---..-.-.-.--..-...-...---.-.--..-.....-.--...-..--..-...---.----.."}

    47

    Returns: {"-----------------------------------------------............................................", "--------------------------------------------..............................................." }

  32. 76

    {".----.-....-.-..--...-.-------......-....-.------.-.....--.--..-----.-.-..-.", ".-.-.-.-.......----.---..--...-.-.-...-..---...-.--.--.---.-.-..---.-.--.-.-"}

    27

    Returns: {"---------------------------.-----------.....................................", "---------------------------.-----------....................................." }

  33. 33

    {".-..-.-.-...--..-.--.-.-....-..--", ".---.-..--.-.-.-.-..-.---.-.-.---"}

    13

    Returns: {"-------------.-..................", "-------------.------............." }

  34. 10

    {"..-------.", "...-..--.."}

    8

    Returns: {"-------...", "---......." }

  35. 64

    {"..--.-....-..-.-.-----.-----..--......---.-........-..-.--.---..", "-.--.....----.---....--.------......-.--..-..-.-.-.--.-..-------"}

    26

    Returns: {"--------------------------.---..................................", "--------------------------.---------............................" }

  36. 91

    {".-....--.......-.-.-...----.--.-------.-.--...--.--.----.-.--..-...-....-.---.-..-...-..-.-", "...----....--.---...--....-.-.---..--.-.---.------..--.--..-.-...---.--.-....-...--..----.."}

    10

    Returns: {".-....--.......-.-.-...----.--.-------.-.--...--.--.----.-.--..-...-....-.---.-..-...-..-.-", "...----....--.---...--....-.-.---..--.-.---.------..--.--..-.-...---.--.-....-...--..----.." }

  37. 98

    {"-..--.--.-.-.-----...-.--.-...--.-.---..-.-..-......--..--...--..-..--.-.-.---.-..-..-.----...-.--", "---....--...-.---..-.---...-.-.-..-..-..--.--..--..--.------...-.-.--.---.....--.--.....---..--.-."}

    32

    Returns: {"-..--.--.-.-.-----...-.--.-...--.-.---..-.-..-......--..--...--..-..--.-.-.---.-..-..-.----...-.--", "---....--...-.---..-.---...-.-.-..-..-..--.--..--..--.------...-.-.--.---.....--.--.....---..--.-." }

  38. 83

    {"-....-.-.--...-.....--...--...--.-.-.-.---.-..-..--.-....-..--..--...--.-..-.--.-.-", "-.-..----.----...-.--.....-..--.-.-.-------..--.-.--.-.-.----..-..--...-------.-..."}

    69

    Returns: {"------------------------------------...............................................", "-----------------------------------------------...................................." }

  39. 9

    {"...-.-.-.", ".------.."}

    8

    Returns: {"...-.-.-.", ".------.." }

  40. 79

    {"...--.-.---.---.--..-----....-.--..-....-.-......-..-...--.-..--.-----.-.--...-", "-.---.--..-.---...-.----.---....-...------.---..--.-.-..--.-..--...-..--.-....."}

    12

    Returns: {"------------.--------------------------........................................", "------------.-----------------------------....................................." }

  41. 56

    {".-------..-.-...-.-..-..-.-.--......-----...--.----.--..", ".-.--.----...---.-.--..-...--.----...--.-......-.-.-...-"}

    42

    Returns: {".-------..-.-...-.-..-..-.-.--......-----...--.----.--..", ".-.--.----...---.-.--..-...--.----...--.-......-.-.-...-" }

  42. 37

    {".-.------...--.-.-....-.-.-..-.-.-.-.", "....-.--...-.-.--.-..-..--.-----.-.--"}

    16

    Returns: {"----------------.--..................", "----------------.---................." }

  43. 73

    {"......-.---.--.---..-.-.-----.-.-...-.--....-....---....-.--.-..-.--....-", ".-..-------...--.-...-...---.---...----.-..-..-.-------.--..-..---.....--"}

    49

    Returns: {"---------------------------------........................................", "----------------------------------------................................." }

  44. 78

    {"..-.-.--.-......-.-.---..-..--.--.----.----.-.-..-.---..-.---..-.---.--.--...-", ".----...-.-.-..------.-...-.-.--.........----...-.---...---.....-..--.-...--.-"}

    28

    Returns: {"----------------------------.--------------...................................", "----------------------------.--------........................................." }

  45. 45

    {"--.....-.---....-...-..-..--...---.-...----.-", "-----...-.-.-.....------.---.--.-..--..--.-.."}

    35

    Returns: {"--------------------.........................", "-------------------------...................." }

  46. 43

    {".--...--...--..----..-..--..-...-----.--.--", "-.--.-...-..-.--..--..-..--.-.-.--...-.--.."}

    2

    Returns: {"--.---------------------...................", "--.------------------......................" }

  47. 7

    {"-.-...-", ".-.---."}

    1

    Returns: {"-.--...", "-.---.." }

  48. 27

    {".--..----.-....-.--....---.", "---..-.-.----..-..---..-..."}

    5

    Returns: {"-----.--------.............", "-----.---------............" }

  49. 19

    {"...----.--...--..-.", "--..-.-...-..-----."}

    18

    Returns: {"...----.--...--..-.", "--..-.-...-..-----." }

  50. 61

    {"--..---...-..-....---.--..--...------...-.---.-.-..-..---....", "-.-.--..--.----..-...----.-----.--..-..-....---...-..-.-..-.."}

    34

    Returns: {"------------------------------...............................", "-------------------------------.............................." }

  51. 49

    {"--.-----...-.-...--...-.---..-.-...-.--..-..----.", ".-.-...-..-.---.-----..--.....--.-..-..-..---..--"}

    36

    Returns: {"-------------------------........................", "------------------------........................." }

  52. 82

    {".-..----.-...--.-..-.-.------.--..-.--.....-.-.--..-.---.---.-...-..-.--.-...-.-..", "-...-..----.--.-.--.-.-.--...--..---.---..--.---...--.-....-......-....-.-.---.---"}

    66

    Returns: {"-----------------------------------------.........................................", "-----------------------------------------........................................." }

  53. 68

    {"-...-.-.--.-.......--------.-.-..----...--...-.-.-..-.---..--.-.-.-.", "-..-..----.---.-------..-.........-....-....-.--.----..-----...--..-"}

    43

    Returns: {"-...-.-.--.-.......--------.-.-..----...--...-.-.-..-.---..--.-.-.-.", "-..-..----.---.-------..-.........-....-....-.--.----..-----...--..-" }

  54. 5

    {"....-", "-.---"}

    4

    Returns: {"-....", "----." }

  55. 50

    {".-...--..--..--.-..-.--..-...-...-----.-...-.--..-", ".-.--.--.-.-.-----...-..-.-.-..--...---...---..---"}

    39

    Returns: {"-----------------------...........................", "---------------------------......................." }

  56. 61

    {"-...-...-...--..-..-.-...--.----.-.--.------...-.-.---.-.----", ".-..-..---.-----..-..-....-.-.-.---..-.....--.--..-.--..-...-"}

    33

    Returns: {"---------------------------------............................", "----------------------------................................." }

  57. 22

    {"-.----..--...-..-.....", "--.-.---.-.--..---...-"}

    16

    Returns: {"---------.............", "-------------........." }

  58. 10

    {"-.-..--.--", ".--.-...-."}

    8

    Returns: {"------....", "----......" }

  59. 92

    {"-..--..-.----.....--..-..-.---..--..---..........--.-..--.--.-.-.--..-.--..-.---.--.---.--.-", "-.-....--..---.-..---...--...--....--.--..-.-..--...---.--......-.-.--.-....----.--.--.-----"}

    12

    Returns: {"------------.----------------------------------.............................................", "------------.----------------------------------............................................." }

  60. 71

    {"...--..--..-.--.---.-.-...--.-.--.-.-...------.----.-.-.-----.-.-..-.--", "-...-..-..-.-.-.-.......-..--.-.-..-.---..----..-..-.......-..-----..--"}

    59

    Returns: {"-----------------------------------------..............................", "------------------------------........................................." }

  61. 9

    {"--..--..-", ".-...-.--"}

    1

    Returns: {"-.----...", "-.---...." }

  62. 52

    {"...--....---.--....-.----...--.----.----.---.-.--.-.", "..-...----......-..---.-.--.--.-.-...-.--...-..-..--"}

    9

    Returns: {"---------.--------------------......................", "---------.--------------............................" }

  63. 66

    {"---....----...-..--.-..----.---..---....---.--.--..---.....-.....-", "-...----.------.---.....-...-.-..-.---...--..-.-..--.-.-.-...-.--."}

    33

    Returns: {"---------------------------------.................................", "---------------------------------................................." }

  64. 14

    {"...-.------...", "-..----....--."}

    5

    Returns: {"-----.--......", "-----.--......" }

  65. 39

    {"--........-....-.-.-.--.-..-...-..-..--", "----.-----.-.-...---....------.---.--.."}

    24

    Returns: {"--------------.........................", "------------------------.-............." }

  66. 71

    {"..---.-...---.--.--.-----....--.-.---.-....-.--.-..-..-.-.----.---..-..", "..----..-.......--.--.--.---.--.-..-...---..-..--.--....-...-----..-..."}

    61

    Returns: {"--------------------------------------.................................", "---------------------------------......................................" }

  67. 97

    {".--.---.-....------.-.-..---..-.----.------.-....-..-..--.---.-....--.-.---....-.--..-....--.--.-", ".-.--.----.-....-.-----...--.....-.--..----.--...-.--...-..----.--..-...--.-.-...--.....-.....-.-"}

    52

    Returns: {"----------------------------------------------------.............................................", "---------------------------------------------...................................................." }

  68. 79

    {"-.-.--.--...-...-....-.----.--.--.......-..-.--.-.---...-..-..-...--..-.-.-...-", "-----.--.--.--..-....-....--..---.---.--.-----..-..-..-...-.--.-.-..-------.-.-"}

    12

    Returns: {"------------.----------------------............................................", "------------.---------------------------------................................." }

  69. 21

    {"-.---..---....--.---.", "..--...--..--..--...-"}

    3

    Returns: {"---.---------........", "---.------..........." }

  70. 44

    {".----.---..-.--.....---...--..--.--...-.--.-", ".-..-----.---..-.-.--.-.-...--..-.-...-..-.."}

    11

    Returns: {"-----------.------------....................", "-----------.----------......................" }

  71. 90

    {".-.-----..---.......--.-..-..----.----..--...-......-.---.-.--...--..-...-...--..-.---.--.", "-..----------..-.-..--..-....----.--..--.--..-..--.....-..--.-.--.------.-..-...---..-...."}

    41

    Returns: {"-----------------------------------------.--..............................................", "-----------------------------------------.------.........................................." }

  72. 68

    {"--........-.-.----..----.---..-.-..--..--..----..-....---...--..-.-.", "-..--..-..-....--..-.---.------.---..-..----.-.--.--..-.---.....-..."}

    22

    Returns: {"----------------------.-----------..................................", "----------------------.-------------................................" }

  73. 15

    {"--.---..-..-.--", "...--..-.--.-.."}

    3

    Returns: {"---.------.....", "---.---........" }

  74. 33

    {"--...---..---....-...--.-..--....", "...---..--.-..-...-.-.---------.-"}

    24

    Returns: {"--------------...................", "-------------------.............." }

  75. 27

    {".-.....----.--.--.-.---....", ".....--...--.--.-.--.---.--"}

    16

    Returns: {"-------------..............", "--------------............." }

  76. 100

    {"...---------------------------------------------------..............................................", "....-------------------------------------------------..............................................."}

    28

    Returns: {"----------------------------.-----------------------................................................", "----------------------------.---------------------.................................................." }

  77. 100

    {"--..-.---........--...----.-.-.--.-..------.--.....--..-..--..-.--....-.--.--.-..-...----.--..-----.", "...-..-------.-..--....-.---.......---...--.-.--.--.--.-..--.--.-.--..-.--.-.---.-..-...-.--...--..."}

    2

    Returns: {"--..-.---........--...----.-.-.--.-..------.--.....--..-..--..-.--....-.--.--.-..-...----.--..-----.", "...-..-------.-..--....-.---.......---...--.-.--.--.--.-..--.--.-.--..-.--.-.---.-..-...-.--...--..." }

  78. 100

    {"..----.....---.---.-.-..-.-..-.--..--.--..----.----...-.----.-.-.------.---.-----.-.--.....----...-.", ".......--.---.-....-......--.-..---.---.-..-.--.-.-....---..-.---.---.-...-..-..-..-.....--..-..--.."}

    67

    Returns: {"----------------------------------------------------------..........................................", "------------------------------------------.........................................................." }

  79. 10

    {"--------..", "........--" }

    4

    Returns: {"----.----.", "--........" }


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: