Giter Site home page Giter Site logo

shortest-path-to-all-keys-algrothim-game's Introduction

The Shortest Path to Get All Keys of a matrix.

Try it here

Introduction

In a game to collect all keys (if possible) in the least amount of moves possible The player must navigate in the matrix and deal with Walls, Locks, and collect keys.

A mathematical model for the solution

We choose Breadth-first search since it has a running time of O(V+E) every vertex and every edge will be checked once.

The Algorithm Analysis

We first setup the game by using nested loops to iterate over all items of the game board. To find the starting point and the number of keys in the game. We then start the queue from the starting point with 0 moves, and the we move in all possible directions while saving the visited nodes in a set (unique) We check before moving if we have already found all keys matching the total number of keys, in the setup and then return the total number of moves if yes. Or If we exhausted all moves (the queue is fully popped) we return โ€œ-1โ€ We add each possible move to the queue and before that we check if moving to that place makes contact with a wall, key, or empty space (valid move) Basic Operation: Comparison

The time and space complexity

Example Game board = [ ['D','@','.','C'] , ['c','.','#','d'] ] Time complexity

  1. In the first loop we iterate over the game board to find all keys and starting point in this case n2
  2. Then for every possible move for the queue element 4 ( directions ) 4 * n2 we check the surroundings So, we get O( n * m)

Space complexity O(|V|) at most we have all vertices in the queue.

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.