Advertisement
Filip13

dfs - lavirint

May 21st, 2024
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | Source Code | 0 0
  1. namespace pretraga_u_dubinu___dfs_depth_first_search_
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int[,] polja = new int[,]
  8.             {
  9.                 {0, 0, 0, 0, 0 },
  10.                 {0, 0, -1, -1, -1 },
  11.                 {0, 0, -1, -1, 0 },
  12.                 {0, 0, 0, 0, 0 },
  13.             };
  14.  
  15.             Stack<int> poljaZaObilazak = new();
  16.  
  17.             poljaZaObilazak.Push(0);
  18.  
  19.             int[,] validniPotezi =
  20.             {
  21.                 { 0, 1 },   //desno
  22.                 { 1, 0 },   //dole
  23.                 { 0, -1 },  //levo
  24.                 { -1, 0 },  //gore
  25.             };
  26.  
  27.             while (poljaZaObilazak.Any())
  28.             {
  29.                 int pozicijaPolja = poljaZaObilazak.Pop();
  30.                 int redPolja = pozicijaPolja / polja.GetLength(1);
  31.                 int kolonaPolja = pozicijaPolja % polja.GetLength(1);
  32.  
  33.                 for (int i = 0; i < validniPotezi.GetLength(0); i++)
  34.                 {
  35.                     int noviRed = redPolja + validniPotezi[i, 0];
  36.                     int novaKolona = kolonaPolja + validniPotezi[i, 1];
  37.  
  38.                     if (noviRed >= 0 && noviRed < polja.GetLength(0) && novaKolona >= 0 && novaKolona < polja.GetLength(0) && polja[noviRed, novaKolona] == 0)
  39.                     {
  40.                         int novaPozicija = noviRed * polja.GetLength(0) + novaKolona;
  41.                         poljaZaObilazak.Push(novaPozicija);
  42.                     }
  43.                 }
  44.  
  45.             }
  46.  
  47.  
  48.         }
  49.  
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement