Problem Statement
Little John likes to play driving games. One of his favorite games involves a horizontal three-lane road divided into cells. The cells are numbered 0, 1, 2, etc. from left to right, and the lanes are numbered 0, 1, 2 from top to bottom. The car is initially located in the middle lane in the leftmost cell (lane 1, cell 0). The goal of the game is to reach any lane of the rightmost cell without hitting any obstacles or leaving the road.
The car moves horizontally one cell at a time. Each time it moves to the next cell, it can either stay in the same lane or move diagonally to an adjacent lane. More formally, if the car is in lane i, cell j, then after the next move, it can be in one of the following lanes of cell j+1: i, i-1 (only if i>0), or i+1 (only if i<2).
The road is given as a
Definition
- Class:
- DriveCar
- Method:
- minNumberOfDrifts
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minNumberOfDrifts(String[] road)
- (be sure your method is public)
Constraints
- road will contain exactly 3 elements.
- All elements of road will contain the same number of characters.
- Each element of road will contain between 1 and 50 characters, inclusive.
- Each character in each element of road will be '.' or '#'.
- The leftmost character of element 1 of road will be '.'.
Examples
{"....", "....", "...."}
Returns: 0
John doesn't need to change lanes to win the game: .... .... .... .... start C... -> .C.. -> ..C. -> ...C .... .... .... ....
{".......",".......","..#...."}
Returns: 0
{"....","....","...."}
Returns: 0
{".....",".....","....."}
Returns: 0
{"........","........","........"}
Returns: 0
{"...#......#..................",".......#................#....","......#...............#...#.."}
Returns: 3
{"........#..",".........#.","........#.."}
Returns: 1
{"..........","..........","..#.#....."}
Returns: 0
{".......#..","..........",".........."}
Returns: 0
{".........#...#.......",".#.........#..#......",".....#......#..#....."}
Returns: 5
{"..........","..#..#....",".......#.."}
Returns: 1
{".....#..","........","#......."}
Returns: 0
{".",".","."}
Returns: 0
{".#..","....","...."}
Returns: 0
{".......","..#....","......."}
Returns: 1
{"......","......","......"}
Returns: 0
{"..","..",".."}
Returns: 0
{"###",".##","###"}
Returns: -1
{"#.###.##", ".#.#.#.#", "###.###."}
Returns: 7
#.###.## #C###.## #.###.## #.###.## #.###.## #.###C## #.###.## #.###.## C#.#.#.# -> .#.#.#.# -> .#C#.#.# -> .#.#.#.# -> .#.#C#.# -> .#.#.#.# -> .#.#.#C# -> .#.#.#.# ###.###. ###.###. ###.###. ###C###. ###.###. ###.###. ###.###. ###.###C -> end
{".........",".....#...","........."}
Returns: 1
{"..#.###","......#","...#..."}
Returns: 1
{"....#...#..#..#.#...",".......##..#...#....","#..#..#....#..##...."}
Returns: -1
{"..#....#..#.#...#..", "...#.#...#.#..#..#.", ".##..........#...#."}
Returns: 5
{"....#..#.",".........","........."}
Returns: 0
{".......",".#.....","...#..."}
Returns: 1
{"...#.......","........###",".#...#....."}
Returns: 1
{".#.............##",".................","........#..#....."}
Returns: 0
{"..##.#.#.#..###.......","..........#...........","...#...#..##....##..#."}
Returns: 2
{"..#....#..#.#....#.....#","...####...#..#.#...##...","#..#...#####.........#.."}
Returns: -1
{"...##.............#...","...#.#.....#....#.....",".##.....#....#.#....##"}
Returns: 4
{".........",".#.......","........."}
Returns: 1
{"#..#.#.##.", ".#.#...##.", ".###.##.#."}
Returns: -1
John can't reach the end of the road.
{".#.#.#.#....####..##..","..###..#...#.##...#...","#.##...#......#.###.##"}
Returns: -1
{".###.#.#.........#.#.#..#....#....","........#.....##...#.........#.##.","......#..###..#..#..##....#......."}
Returns: 5
{"...#.........#","....#..#......",".#..##.....##."}
Returns: 2
{"#...#.....##....###..####..#",".####....#.##...#.###...#.#.",".#.#...#....#..######..##..."}
Returns: -1
{"#..##.#...##....#..###.","..##..#..###.#....#.#..","..##...#.#.##...##..#.#"}
Returns: -1
{".....#........#..","..#.#............","....#.......##.#."}
Returns: 2
{"#.#....#.....##","...#.#..#.##...","#...#.#..##..#."}
Returns: 4
{"...........................","...........................","..........................."}
Returns: 0
{"#.######...#.","..##.#.######","###.#.##.##.."}
Returns: -1
{"##.##.....###....#####...#..#######..#.###.##..","......#....#..#.#..#.#....#..###.....#.......#.","..##...###.....###.##.#.#.#..##.##.#...#.###..."}
Returns: -1
{"...",".#.","..#"}
Returns: 1
{"..#....",".....#.","#...#.."}
Returns: 1
{".#.#...#","....####","...##..#"}
Returns: -1
{"..#.##.#.#.#...###.##.#.######.#..####.#..####",".####.#.#.#####..##.#.####..###..##.#######.#.","#####.#.#####.###..#########.#####..###.#.####"}
Returns: -1
{"...........#","......#...#.",".....#.#.#.."}
Returns: 2
{"###..#.#...#....#.#...##......#..#.....","...#..#.....#.##.##..#....###...#.##...","....#.............#....#.#...#........."}
Returns: -1
{".#.#####.##","..######..#",".#....###.."}
Returns: -1
{"####.#..###.",".##..##..#.#","...##.##.#.#"}
Returns: -1
{"######.##########.####.##.#####.#.###.#.####.#####",".##.########.######..##.######.#..#..##..#########","#############.###.##.##########.#####.#######.####"}
Returns: -1
{"......................................#...........","....................#....#..............#.#.......",".................................................."}
Returns: 1
{"##.#####.######.####.######.#...#.########.##.#.##",".##..#########.###..######.#.###.###.#.##..#.#####","#..##....#...####..####.###.########...##..#.##.##"}
Returns: -1
{".#.#.#####.##.######.#.#...#.##.#...#..#.#########",".##.########...#######.####..##.#####.#..#.###..##","####..#.########..###.#.###.##...####.######.#####"}
Returns: -1
{"######.######################.##########.###.#####",".########################.###..#####.#############","#..##############.###########.######.#.###########"}
Returns: -1
{"#.###.###.###.###.###.###.###.###.###.###.###.###.",".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..","###.###.###.###.###.###.###.###.###.###.###.###.#."}
Returns: 48
{"...#.....##......#..#.#.......",".......#......................",".....#........###...#....#...."}
Returns: 2
{"......#.......#....#.....#..........#........#.","..#...........#.........#................#.....","......................#........................"}
Returns: 3
{"....#.#..........#..#...##.......#.#..#",".....##.##.#.#..........#..##..........",".#...#.#......#..#..##......#.#.#..##.."}
Returns: -1
{".#.....#...#.##.#.#.........#.#.#...#.....#...#..",".....#..#..##.##.....####..#.##.#.#............##","....#...#...##........#.####.#...#....#.......###"}
Returns: -1
{ "..#", ".##", "##." }
Returns: -1
{"............#.............#.......#......#.....#..", "....#...#.......#...#....#....#...........#....#..", "...............#........#.......#...#.....#......." }
Returns: 9
{".#.", ".#.", ".#." }
Returns: -1
{"..................................................", "..................................................", ".................................................." }
Returns: 0
{"..", ".#", ".#" }
Returns: 1
{"......#...........................................", "..................................................", ".................................................." }
Returns: 0
{"...", ".##", "###" }
Returns: 1