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

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

License

Notifications You must be signed in to change notification settings

FANMixco/Breadth-First-Search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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