Problem Statement
Given a regular grid of size W x H with various squares blocked out, find a path from point (0,0) to point (W,H) that does not pass through any of the blocked-out squares. The path must consist only of 90-degree arcs (quarters of circles). The radius of each arc must be an integer, and therefore begin and end on an integer grid point. The tangents at each end of every arc must be parallel to the x or y axis.
Arcs may intersect the corner of a blocked-out square and even pass between two that share a corner (as shown in the examples below), but may not pass through the interior of any blocked-out square.
The grid will be given as a
{ "....########", "###..###...#", "..##..#.##.#", "...##..#...#", "....#..#...#", "....#..###..", "....####.##.", "..........#." }
there is a path from (0,0) to (12,8) consisting of 4 arcs, as shown in the following figure:

Find the path that contains the fewest number of arcs, and return the number of arcs in that path. If there is no path, return -1.
Definition
- Class:
- Arcs
- Method:
- numArcs
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int numArcs(String[] grid)
- (be sure your method is public)
Constraints
- grid will contain between 1 and 50 elements, inclusive.
- Each element of grid will have a length between 1 and 50, inclusive, and will contain only the characters '.' and '#'.
- All elements of grid will have the same length.
Examples
{ "..", ".." }
Returns: 1
A single arc of radius 2, centered at (0,2) or (2,0), forms a path from (0,0) to (2,2).
{ "...", "..." }
Returns: -1
There can only be a path between opposite corners if the dimensions of the grid are both even or both odd. In this case, the dimensions are 3 x 2, so no path exists.
{ "....", ".##.", ".##.", ".##.", ".##.", "...." }
Returns: 7
Seven arcs of radius 1 are required in this example.
{ "." }
Returns: 1
{ "....########", "###..###...#", "..##..#.##.#", "...##..#...#", "....#..#...#", "....#..###..", "....####.##.", "..........#." }
Returns: 4
This is the example in the problem statement.
{ ".....#................", "....#.................", "....#.................", "....#.....#...........", "....#.................", "....#....#.#..........", "....#.......#####.....", "....#.....#.....#.....", "....#.....#.....#.....", "....#....#.#....#.....", "....#....#.#.....#....", "....#....#.#......#...", "....#....#.#.##.#..###", "....#....#....#......." }
Returns: 5
There is a path consisting of 5 arcs, as shown in the following figure. Notice that the path may cross itself:
{ ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", ".................................................", "################################################.", ".................................................", ".################################################", "................................................." }
Returns: 1177
{ ".#...#...#...#...#...#...#...#...#...#...#...#...", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.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#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", ".#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.", "...#...#...#...#...#...#...#...#...#...#...#...#." }
Returns: 1177
{ "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", ".................................................." }
Returns: 1
{ ".#................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "................................................#." }
Returns: 2
{ "........................", "........................", "........................", "........................" }
Returns: 7
{ "............................", "............................", "............................", "............................" }
Returns: 7
{ "............................", "............................", "............................", "...#........................" }
Returns: 9
{ ".................................................." }
Returns: -1
{ "..................................................", ".................................................." }
Returns: 25
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }
Returns: -1
{ "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", ".." }
Returns: 25
{ "................................................." }
Returns: 49
{ ".................................................", "................................................." }
Returns: -1
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }
Returns: 49
{ "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", ".." }
Returns: -1
{ "..#......#...###.#.....#..#.......##.#..", ".......#..#.#...#.......###.......#.##..", "#..#.....#..............#.....#...#.#...", ".#...#...#............#.....#.##......##", ".........#....#......................#..", "...#...#..........#..#..#..........#....", "....#.#.........#.##........#....#......", "#.......##..#...........#.......#.......", "...#.......#.......#.....#....##.#.....#", ".....#......#.#.......#.....#....#......", ".....#....#...#.#.......................", "......#.............##........#.#......#", "..#.#.#..........#......#.........#.....", ".#......#..#..#...#.#.#.....#...##......", "#......#.#............##....#..#..#..##.", "..#...............#............##.......", "##.......#.##...##.....#.........##.#..#", "..###............#.....#...........#....", "...##.............#.#...............#...", "..###..#...##.....#.##.#............##..", ".........#.............#.....#........#.", ".#........#.....#.....#.#.........#.....", "#.##.##....#..#..#....#........#.#......", "..#.............##..#..#.....#......#...", "##..##...##....#.....#..#.#.....#.......", ".....#.##......#...#........#..#........", "....#.##......##.#..#.#.#.....##....#...", "#..........##.#..#.........#.#..##.#..#.", "#...#..#..#....................#.......#", "##..#..#............#.#...#......#..#...", "........#.#.......#........#...#....#...", ".....#.........#.....#...#.#.....###....", ".......#.###..#.........#.........#....#", "..##...##...#.......####....#........#..", "...........#.........#.....#.........#..", "..##..#...#.#..#..................##....", "....##........##.......................#", "#.#........#........#........##.........", "..#..###.....##........#.....###..#.....", ".##..........#......#.#.#.#....##.#.....", "....###.....#................##..#..#..#", ".#........#.......#.....#..#.......#.#..", "#.............#......#............#.....", "#....##..####..#......#.#...#...........", "..#..#...#..#...#.....#.......#.#..##...", ".....###.#..#............#.........#.#..", "....#...........#.....#.....##......#...", "....#...#.##..#..#....#..#.##.........#.", ".#..#...........#....####.#......#......", "......................#..............##." }
Returns: 10
{ "..............#................#..#.#....#...#.#..", "...#......#...#.#....#...#...............#...#.#.#", "..#...............................#............#..", ".....#...#.##..#......##........#..#...#.....#.#..", ".##..#..##...........#.....#......#....#.....###..", ".#..#...........#............#...#.....##....###..", ".......#...#.......#.......#........#..#..#..###..", ".....##...##......##.......#...........#...#......", ".#...#..#....#....#..#..........##.......#..##....", "..#.#.#.#....#...........#........#..##......#....", "......#.#..#.#...#.........#.....###.....#........", "............#...........#.........###...#.....#.#.", "###.#...#.....#.....#......#.......#..........#...", "#.....#...##..#...#........#...#.............#.#..", "#..............#.......#...#.#...##.##..........##", "...........#.........#..#......##........#..##....", "........#...###...###...........#..##..#..........", "...#..............#.....#.#.#.#..#....#......#....", "#.#..#.......#..............#...#.........###..#.#", "..#.#......#..................#....#..###.........", ".....#.#.....##...#.......##.......#.#....#.......", "..#.#..#...#..#...............#....#...#.#...#....", "...........#......#....#.......##...#...#...##...#", "....#....#................#......#.#..#.#...#.....", "..#....#..#..#.###.........##.......#......#.....#", "..###....#..#.##..#.##...........#......##......#.", "..........##.........##...#.....#....#.....#.#.#..", "...........#...........#.#..#....#..#.........#...", "###....#..#.......#....###..#...#..#..#..#..#.....", "........#...##...........##........#....##........", ".#..#.........#......#.....#.#...#......##.#.....#", ".....#.......#....#....#..#...#..#...........#....", "........#...#.#..........#.....#..#...#.#....#...#", "..#........#.........##.##...#.##....#.##..#......", "#...#....#.#.....#...#.......#...#........#.#.#...", ".#.##.#....#...#..##...#...#...........#.#........", "..#..#......#..#..#..#.....#.##...#.......#...#...", "...#.#....#.##......#.......##..................#.", "#.......##......#......#....#...#....###...##.....", ".#...#....#............#...#.....#..............#." }
Returns: 8
{ ".##....##.#.#......#.....#..###..##...#....#......", ".....#....###........#.........#.##.......#.#.##..", ".......#....#.......#...#.#.......###.#.........##", "...#......#...#...#.#.......##...#....#.......#...", "..........#....###...........#.......###...#..##..", "..........#....#...#..##..#.##..#..#...#..........", "...#......#...........#..#.........#.............#", "#.......#....#.#....##.#.#...#.......#.....#.#....", "......#.....#....#.....#........#....#........#...", ".#..#.....#...#..#...#........#...##........#.#...", "....#..#..............#.....#.#...##.#..#..#.#....", "...#...#..........##..............#.#.....#.....#.", "#.#.##.##......#.........#...##......#.........#..", "....#.#.##.....#...#....#....#....................", "..#......#..#.##.......#.#...#.#...............#..", "..#.#................#....##...#.....#..........#.", "..........#.....#........#...........#.#........#.", ".#......#.......#....##...#...#...##..#.##......#.", ".#..#........##.#...##.........#.#.......#.#...#..", "...#.......#...#........#.......#................#", ".##..#......#..............#...........#.##...##..", ".#....#....##...........##..###...#.#.....#..#..#.", "........#....#..#......#.#.......#...#..###..#....", "...#....##...#......#...........#.................", "..........#.#.........#.............#...#.#.#...#.", "#..#...........#..............#.#..#....###....#..", "...............##..#.....#......#.........#..#....", ".#........#.........#.#..#.#..#............#......", "....#...#.....#...#......#...##...........#..#....", "...#............#....#.......#.......#...#.....#..", "......#....#....#.#.#...........#..#.#.......#.#..", "....#...##.........#.........##..#...#.....#......", ".........#..##..#.......#.#.#..#...###....##...##.", "...#......#..##............#.......#......#...#...", "..#......................##.#.......#...#...#.....", "#.#.......#...##........#......#......###..#..#...", ".....#....#...##......#.##.......#...............#", "......................#.#.............#..........#", "....#.........#.....#......#.#...............#....", "#......##................#..##...#...#.........##.", "....#..........##........#.........#.......#......", ".......#......##..##.....#...............#.#...#..", "#......#.....#.......#..#........#.#.#......#....#", ".........#.......#.........#.......##...........#.", "#....#..................#......#.#....#...........", "....#....#.............#...#.#..#.......#..#......", "........#...#...........#................#..#.#...", "#......##....#....................#.##...#.##.....", "...#.......#......#..#..#.....#.........#...#....#", ".....#.#..#......#......#...#......#...###........" }
Returns: 7
{ "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", "..................................................", ".................................................." }
Returns: 1
{ "....", ".##.", ".##.", ".##.", ".##.", "...." }
Returns: 7
{ ".......#.#..............##........................", ".............................#.........#..#..#....", ".................#......#.......###.......#..#....", "#.....#.......#...#.#.#....#.....#....#.....###..#", "......#....................#...#....######...#..#.", ".......#.#....#.........###.......................", "#.#..........#.............#...#..........###...#.", "............#...#.............##...#....#.#.......", "..##....#.#.......#....#..........#.#.........#...", "..#.#.#.#...........##..#..........#......#.......", "..#.............#...........#.......#.......#.....", "........##...#............##..........#.....#.....", ".#.......#......#.#.......#.#..#............#.....", ".......##.........#................#............#.", "...............#......#....#...#..#....#.##.......", "....##...............##.#....#.....#..........#...", "...##...#....#...##....#..#..#........#......#....", "..#.#...........#.....#.......#.............#.....", "..........#..#..#....##..#.#....#...#.............", ".........#.#....##...#..#.......#.#..#............", "......................#...#...###....#.#.......#..", "......#.....#........#..........#.......##.#......", "..#...#...#...#.#....##...........#......#.#......", ".##....#......#...#.................#........#..#.", "............#................##...#..##......#....", "........###.........#.............#........#.#....", "...#.....#.............#.....#....#.........#....#", "......#................#.#.....#.......#..........", ".....#...#.....##.........................#......#", "........#..........#....#...........#.........#.#.", "......#..........................##....#.#........", ".................###.........##...................", "....#..#....#........#...........#..#.#......#....", ".##............#......##......#...............#...", "#....................#....#........#.#.##.#......#", "#.#....#......##..#.#...#..##.#...#..#...#........", ".#.#.......#....#.....#....#.......#........#.#...", ".....................#.................#..........", ".#...#................#..#.......#.............#..", ".....#.........#.#...#..........#.#..#.......#..##", "........#...#.....#............#..#............#..", ".....#.....#............#.........................", "#.#.#...#......................#.......#..........", "....###.................#.#....#....#........##...", "..#.......##.#.##...........#.....#..#.#..........", "........#..#..#....#..#.....#..#.#..........##....", "..........#..#..##...#....#.............#.........", "...........#....#........#.....#.##......#........", "............#........#........#..#......#....#...#", "........##..#......#...##......###.......#......#." }
Returns: 7