I came across this question on StackOverflow listed under Dynamic Programming, but there didn’t seem to be an accepted solution with an explanation — so I figured I’d give it a shot and document the solution along-with my thought process.

Enjoy!

The Problem

There is a character who can move around on a two-dimensional _(x,y)_coordinate grid. The character is placed at point (0,0).

From (x, y) the character can move to (x+1, y)(x-1, y)(x, y+1), and (x, y-1).

Some points are dangerous and contain land mines. To know which points are safe, we check whether the sum of the digits of abs(x) plus the sum of the digits of abs(y) are less than or equal to 23.

For example, the point (64, -59) is not safe because 6 + 4 + 5 + 9 = 24, which is greater than 23. The point (105, -17) is safe because 1 + 0 + 5 + 1 + 7 = 14, which is less than 23.

How large is the area that the character can access?

The Solution

Before we code this solution, it’s important to understand the coordinate system of a graph in the real world vs its programmatic representation.

The Coordinate System

To start off you first need to remember that an

Arraycannot have negative indices, therefore in order to create a graph that has both negative and positive coordinates you need to have an array twice as long as the0..nlength of an axis. For example, if you need the_x_-axis to range from-100..100you will need an array of lengthAXIS_LENGTH * 2to accommodate these coordinates whereAXIS_LENGTH = 100.

Since a graph is technically a data structure represented using rows and columns, we can easily represent it using a 2D array.

#java #competitive-coding #graph-theory #coding-challenge #maximum-accessible-area #2d-grid #hackernoon-top-story

How to Find the Maximum Accessible Area on a 2D Grid
2.60 GEEK