Giter Club home page Giter Club logo

distanceab's Introduction

PROBLEM

Given a 2-dimensional grid with 'A', 'B', and 'O', find the shortest distance between any 'A' and 'B', where the path must not go diagonally.

Grid sample

SOLUTION

One of the optimal solutions is quite simple - for every single point on the grid find the shortest distance to any 'A' and the shortest distance to any 'B'. Then at every point on the grid the sum of distances to closest 'A' and closest 'B' gives the shortest distance between any 'A' and any 'B' at that point. The smallest sum is the answer to the problem.

Instead of calculating the entire distance map, the provided code uses 1-row distance map. The algorithm does a row-by-row scan, thus it only needs to refer to the closest 'A' or 'B' from the row above. Then, as it scans a row itself, it overwrites the 1-row distance map with newly calculated values for the current row.

Distance map

As shown above, the numbers on distance maps for 'A' and 'B' are only accurate if you look at one row at a time and ignore rows below. The sum of A and B distances at any pair of highlighted cells gives the answer of 2 in this case (except the pair of 6+1).

PERFORMANCE

Such optimized solution performs the best due to the approximate complexity of O(1) and sequential RAM access patterns.

Perfomance comparison 500x500

The results above are shown for a 500x500 grid.

distanceab's People

Contributors

kind-serge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.