Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Latest commit

 

History

History
47 lines (26 loc) · 1.74 KB

README.md

File metadata and controls

47 lines (26 loc) · 1.74 KB

Breadth-First-Search

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.

Given a matrix of N*M order. Find the shortest distance from a source cell to a destination cell, traversing through limited cells only. Also you can move only up, down, left and right. If found output the distance else -1.

  • 3 represents ‘source’
  • 4 represents ‘destination’
  • 0 represents cell you can travel
  • 1 represents cell you can not travel

Examples:

Input :

    {1, 0, 1, 3},

    {0, 1, 0, 0},
    
    {1, 0, 0, 0},
    
    {4, 0, 0, 0}

Output : 6

Input :

     {1, 0, 1, 3},
     
     {0, 1, 0, 0},
     
     {1, 0, 0, 0},
     
     {4, 1, 1, 1}

Output : -1

This is a C# implementation based on:

https://www.geeksforgeeks.org/shortest-distance-two-cells-matrix-grid

Follow me at:

LinkedIn YouTube Amazon Goodreads Instagram Cyber Prophets Sharing Your Stories
LinkedIn YouTube Amazon Goodreads Instagram RedCircle Podcast RedCircle Podcast