Problem Statement
Some people leave the lights on when they leave their workplaces. What a waste of resources! Lucyanna waits until all students and professors leave the university building, then goes and turns all the lights off.
Viewed from the side, the university building is a rectangle of cells with N rows and M columns. Each row represents one floor. On each floor, the leftmost cell and the rightmost cell contain stairs, and all other cells are rooms. Only rooms can have their lights turned on and off. Floors are numbered starting from 0 at the bottom. Cells on a floor are numbered starting from 0 on the left. (Thus, cells 0 and M-1 are always stairs.)
Lucyanna starts her quest in the cell (0,0). Her goal is to turn off all the lights and return to the cell (0,0).
In one move, Lucyanna can go from her cell to an adjacent cell in the same row. If her current cell contains stairs, she can also move one floor up or down, if there is another floor in that direction. Each movement takes one minute.
Lucyanna can only turn off the light in the room in which she currently stands. This also takes one minute per room.
You are given the
Definition
- Class:
- TurnOffLightsDiv1
- Method:
- getMinimumTime
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int getMinimumTime(String[] building)
- (be sure your method is public)
Notes
- Lucyanna is not required to visit all cells.
Constraints
- N will be between 1 and 9, inclusive.
- M will be between 3 and 47, inclusive.
- building will contain exactly N elements.
- Each element of building will contain exactly M characters.
- No element of building will contain characters other than '#' and '.'.
- The first and the last character of each element of building will be '.'
Examples
{".##."}
Returns: 6
One optimal solution: Move right, turn off the light, move right, turn off the light, move left, move left.
{".....",".#..."}
Returns: 5
The only optimal solution: Go one floor up, move right, turn off the light, move left, go one floor down. Note that element 0 of building represents the bottom floor where you start.
{".###.",".....",".#...","...#."}
Returns: 21
{".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################."}
Returns: 742
{".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################.",".#############################################."}
Returns: 879
{"....","....","...."}
Returns: 0
{".........",".#######.",".#.....#.",".#######.",".#.....#.",".#######.",".#.....#."}
Returns: 81
{"....#.#.........#..#.#.#..##.##....#...####....",".#...#...#.#....#....#.####..#...##..#....#....","......#.##.#.#........#....#####.....#...#...#.","...#...#......#.###...#......#.#.#...####....#.","......#..#....##.....#.###..#..#..#.###..##....",".#.#...##...#..#..##.##..#.......########...##.",".#.#..#.....#...#.#.#.###....#.........#...#...","...##..#..#.#.#.#...#...#...#..........#####.#.","..###.#......###.#...#.##.#..#........#..#..#.."}
Returns: 595
{"...#..##.#.#.###.....##...#..##.#....##..#.....","..#..###..#...#.#.....#......##.#...........#..","...##........#.....#.......#.#.##.#..#.#..#..#.","..##........#...#.#..........#...#..##.##...#..",".#......#.............#...#.#..#.##..#.#...##..","...##.......##....#.##..#...##..#..#.......#...","......#..........##.#...###.#.#.#....##.##...#.",".#..#....#..#...#..#.#........#...........##...",".##.#..####.......#.#..........#.....#.#..#..#."}
Returns: 568
{".##.###.##....##.......#..##..#..#.#.......#...",".#........##..####......##...#.....##....#.#.#.",".#.####.###.###....#.#....#.....#...#.###..#.#.","....#####...##........#.#.....##....#...#..###.",".......###.#...#.#...##................#.#.#...","....#.#....#.#......###.....##..#.....#...##.#.",".#........##......##..#......#.#....#....#.....","............##...#.#.....#.#.#........#####.#..","....#....#..#....##....#...#......#.#..##.#...."}
Returns: 572
{"........####.......#.##...#..#.#...#........##.",".#.#.#.....................##..#.#....#..###.#.","...###....#.#.##.#.#...#..#....#....#.....#....",".#..#...#.....##..............###..#.##....##..",".......#.###.###..#..#.#.#.............#.....#.","..##....#.....##...#....#......#.......###..#..","....#.#......##.##...#.#.#....#.......#.#....#.","..##.#...##.#..#.#.#....#.#......#......#.##...","..#.##..##......#.##................#.##......."}
Returns: 548
{"...#....#..#...#.......##......#...#.##..#.##..","..#...#..#......#..#..#..#.#.##....##....##..#.",".#.##...#.#....#.#...##...#....#........##.#...","......#.#..#.####.#..#..#......#..##....#......","....#.###.#......##...##......##..###.##...#.#.","...#...##...#....#.#.#...#..#..#.###.....###...","..#....#.........###.......###............##.#.","...#....##..#...#....#..##.#.##..#...##..##..#.","...........##....#...#...#....#...#...#..#.#..."}
Returns: 580
{".#.#...#...........####.#...#..##...#..##.#..#.","..#..#....##...#......#..........#..#......###.","....#.#..........#.....#.#.##....#...##...#....","...#.#.#.......###..#..#.#..##.#....#.....#..#.",".#.#.###.#.####..#.#...#..#.#.#...##.#...#.#...","..#.##..#..##..#...........#.........#.........",".#....#####...#...#.....#.#####.........##.....","..#.......###........#....#..##.#.......##.....","...##..##..#......#...##..#....#.#..#..#...#..."}
Returns: 576
{"..##..............#....###....#.##..#.##..#.#..","............####.##..##.##.##.........#.###..#.",".#.##............#...........#....#.#...#....#.","...##.##....#.#...#...#....##........##...#....",".#.#...#......###..#.#.#.#..#.....##.....#..#..",".#.##............#..##.#.###..##..##..#..#.#...","........#...#.##....#...#.....#..#......#..#...",".###.......#...#.......#..###..#..#..##...#....","...........##..#.......#...###.#..#...##.#....."}
Returns: 569
{".###........#....#.###..#.#....##..#...#.....#.",".##...##...#..##..#...#...#..#.#.###....####...",".##...#..#.#..#..#..#..###...##..........#...#.",".....#.......#.....#...#.#.#...........##.#....","..##...#..#.#.....#.#...##.##.#.#..#.#.##.##...","............###..###...#..#....#..##...##.###..","....#.##.....#..####.#.##..#.#.#...............",".......#.##.#..#.#....#...#....##.#....#.##.#..",".#......#.#.......#.#.####.....#...##.......##."}
Returns: 582
{".#....##...##.##..###..........###.#..#.#..#.#.",".##.....##...##.....#.#.##..#.....##.....#.###.","..###......#...#..#.##......###.#......#..###..",".......#...#.####......#..####.#.#.#..##..##.#.",".#..###..........#.......##..#....#.##.....#...","...##..#.#...#.###.#..#...#.....#.#.#.#..#.##..","...#.........##...#.#.#...................#..#.","..##......#...#....#...##.##.....####....#.....","......#.#....#.#....#.#....##...#.......#....#."}
Returns: 571
{".##.#......##...#......#.#...#.#.....##........",".###...#..#........#.##.#######..#.........#...","...#..#....#.#.#..#..#...#####...#...#...#.#.#.","...#....#....##.##.......#..#......#...##..#...","..#.........#....##...#.....###.##.#......#.##.",".....#..##...#.#.#..###....#......#..........#.",".....#..........##.##..........##.....#.####...",".........#.......#...#...###......#...#.#......","..#..........###........#..#.............#....."}
Returns: 562
{"....#.##.............................#.....#...",".........##...#.......#.....................#..","........................#..............#.......","...............#..................#............",".....#......#...............#.#......#.........","........#..................................#...","..........#......#.........#..#........#.......",".##....#....#......................#......#....",".............................#................."}
Returns: 415
{"............#.................#..##........#...",".............................#..........#......","..#..#.#....##.....#................#......#.#.",".#....#..............#...#.....................","......................#........................","..............#.#......................#..#....","......#...........#................#...........","........##......................#..............","....#.#...........##......##........#.....#...."}
Returns: 455
{"..................#........................#...",".#....#............#.#.#.......................","...#.....................#.....................",".......#.......................................","......#...##.#...#..#......##................#.","......#...#...#..............................#.",".....................##..#..#..................","........##........#.........#......#.......#...",".....#...#......##..#.......#....#.#..##..##..."}
Returns: 425
{"........#..............................#....#..","........................#....#.................",".................#...#..................#......","..#....#.....................#...............#.",".....#..........###......#.....................","................#...#.....#.#........#...#...#.","...................#..#.........#.........#..#.",".........................#..................#..",".............#.........#......................."}
Returns: 445
{".........................#.....#...............","...#.....##.....#..........#.........#.......#.","....#.#..........##.......................#..#.","...............#........#..#.........#.........",".....#..............#..............#...........","........#......#..........#.....#.......#..##..","..#..#.......#........#.........#..#....#......","...................#..........#..#.............","......................#...#...#................"}
Returns: 470
{"...........##...........##...................#.","..............#.......................#........","...................#.....##...#................",".....#.......................................#.",".........#.....#...............#..##......#.#..","..#.......##....#.................#.##.........","...............................................","....#.....#..........#.......#..##.............",".#.................##..............#..........."}
Returns: 387
{".......#....#.......................#.....#....","..#...#...#...#............................#...",".....#..............#....#.#.............#.....","................#..............................",".......#...................#.......#..##.......","............................#....#.............",".........................................#.....","........#.......#..#...........#..........#....",".#.#...........#..............................."}
Returns: 373
{".#................#.#....#.....................",".......#.........#.#...........................","....#..#..............................#......#.","..#..#..........................#..............","................#..............................","...................#.....#...........#.........","...........#............#.....#....#...........",".......................#........###............","..#............................................"}
Returns: 369
{"...#.....#....#.......#..............##.....##.",".#.............##.....#..........#.........#.#.","..#.....#......#...............#.......#....#..",".........#..............................#......","........#............#...##....#...#........#..","..#...#....................#..#..........#.#...","......#........#.#.#...#...#........#..........","...#.......#.........................#.........",".........#......#......#...........#..##......."}
Returns: 466
{".........##..#.......................#.........","....#..........................#...............","...............#.......#.#...#.###.............","........................#..#.#...............#.","..........#......#.#..............#............",".#...#.........................................",".......#...........#...........................",".....#.#.......#...............................","...................#...............#..........."}
Returns: 390
{"..............#....#....#...#..................",".....#.........................................","....#..#..#.......#...#..#.....................",".......#.#..................................#..","...#.#..................................#....#.","...#.....#........#.........#....#.......#.....","........#...........................#..........","..#....................#.........#.............","..#..............#..#...#.#.........##...###..."}
Returns: 381
{"..#............#.#........#..........#..#......","..................##........#.##......#........",".................................#.............",".#.........#.......#..........#......#.....#...","........................#...................##.","...#...........#......#.#.#....................","....#.......#.......#...#...#........#......#..","....#................#..............##.........","...........#.....#..............#........##...."}
Returns: 453
{"..................#...........#..............#.","........##.......#.................#...........","..#..........#.......#.....#................#..","...#..............#...............#.....#......","....##.#..#.......#............................","...................................#..#........","..............#.........#.#....................","......#.......#.....#..........................",".........#..#.......#.....#.....#..#..........."}
Returns: 427
{"..##.#......#..........#....#..................","...#.......#..#.#..........#..##....#.......#..","................................#.....#........","..............#..#............#..........#.....",".#.........#....#.....##..........#.#......#...",".#...#......##................#................","........#.......#..#............#..#...........","................................#...#..........","..............................................."}
Returns: 387
{".##.#...#........#.....#....#......#....#......","................##........#..................#.","........#...#.......##..............#....#.....","........##.#...........#.......................","........#.....#......#..........#..#...........","..........#.#....#...#.............##........#.","......................#........................","...........#.....#.....#....................#..","..............#....#.......#.#........#........"}
Returns: 473
{".##...........#...........#.....#.","..#...#...#..#.#.#......#.#.......",".....#..........#..........##.....",".....#.#.......#.............#....",".#......#..#..##........##.#.#....",".....###.............##.#.........",".#...#..........#..#.....#...###.."}
Returns: 292
{"..#...#.#...#.#...#..",".....................","...#......#.........."}
Returns: 52
{".#..#....#.#..........#.#..",".....#....#..#.............",".#...........#......#..#...",".............##...##.......",".....##..#..........#...##."}
Returns: 161
{"...","...","...",".#."}
Returns: 9
{"........"}
Returns: 0
{".....#..##....#..##.#....#..#.#..###......#.#..", ".#...#..#......#..#....#.....##...##.#...####..", "..####.....##...#..#..##........#.#.#.#...#....", ".#...#..##..#.#..#...###.#.###.#......#.#.###..", "...#....#.###......#...#..............#..#...#.", "...#......#.###.#..#.....#......#.#.#...####.#.", "..##...#..##..##..#..#........#.#........#.#.#.", "..#........#.#.#####....#..##...##.#.#...#..#..", ".#...#.##.#...#..#...#####...#..##..#.#....#.#." }
Returns: 589
{".#..................#.", ".####################.", ".#..................#." }
Returns: 76