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

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

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

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.


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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Teach Yourself Java: Where to Begin

All languages are “tools” for solving numerous problems. However, there is one that can be considered a star choice — Java (though some would argue for Python).

Learning by Doing: How to Learn Java Basics by Building Your Own Project

Check out some ideas for sample projects that can help you learn Java, and programming in general, in a fun and engaging way.

Access Modifiers in Java Example | Java Access Modifiers

Access modifiers in Java language help to restrict a scope of class, constructor, variable, method, or the data member. Public, Protected, Private, Default.

Top 10 Java Frameworks For Web App Development: 2020 Edition

Java Frameworks are the pre-written code body through which you are enabled to add your own code. There are numerous Java frameworks that have different leading features, on the basis of the same here, I will discuss Top 10 JAVA Frameworks For Web App Development 2020-2021.