Advertisement
skalarr

Untitled

May 28th, 2022
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 7.53 KB | None | 0 0
  1. # LAVIRINT
  2. # K4, 1. SAMO GRAF(trazi sve putanje preko dfsa),2. primena svega toga u lavirintu(prebrojavanje, obicna pretraga sa bfsa)
  3. # get path, u jednom od ta dva
  4. # 1x5, prva vrsta
  5. # zelena polja su zidovi, zanemarujemo prilikom pretrage
  6. # BFS je nakraca pretraga
  7. # kovceg sa blagom i rupe
  8. # matricu susedstva sami pisemo
  9. # pozive isto sami kucamo
  10. # prvo includujemo skripte (ne napamet), 3 skripte (sablon, uslov, poziv), definisemo velicinu lavirinta
  11. # generisemo graf metodom (emptyLabyrinth), nakon toga definisemo tipova polja(max 4, zavisi koliko tipova imamo),
  12. # poziv koda, BFS, DFS, get path(ako se trazi), onda ispisujemo lavirint
  13.  
  14. #lavirint pr 8.1
  15. mutable struct Cvor
  16.     color::Char
  17.     type::Char
  18.     d::Int
  19.     pred::Int
  20. end
  21. mutable struct labyrinth
  22.    rows::Int64
  23.    cols::Int64
  24.    AdjMatrix::Array{Int, 2}
  25.    V::Array{Cvor, 1}
  26. end
  27.  
  28. function generateTableAdjMatrix(nbRows, nbColumns)
  29.     adjMatrix = zeros(nbRows*nbColumns, nbRows*nbColumns);
  30.     adjMatrixRowCounter = 0;
  31.    
  32.     for i = 1:nbRows
  33.        for j = 1:nbColumns
  34.            currentId = (i-1)*nbColumns + j;
  35.            neighbourRow = [currentId-1 currentId currentId+1];
  36.            neighbourMatrix = [neighbourRow .- nbColumns; neighbourRow; neighbourRow .+ nbColumns];
  37.  
  38.            startRowIndex = 1;
  39.            startColumnIndex = 1;
  40.            endRowIndex = 3;
  41.            endColumnIndex = 3;
  42.  
  43.            if i == 1
  44.                startRowIndex = 2;
  45.            end
  46.            if j == 1
  47.                startColumnIndex = 2;
  48.            end
  49.            if i == nbRows
  50.               endRowIndex = 2;
  51.            end
  52.            if j == nbColumns
  53.               endColumnIndex = 2;
  54.            end
  55.  
  56.        neighbours = [];
  57.            for m = startRowIndex:endRowIndex
  58.               for n = startColumnIndex:endColumnIndex
  59.                   if neighbourMatrix[m,n]!=currentId
  60.                     push!(neighbours, neighbourMatrix[m,n]);
  61.                   end
  62.               end
  63.            end
  64.  
  65.            adjRow = zeros(1, nbRows*nbColumns);
  66.            for m = 1:length(neighbours)
  67.                 adjRow[neighbours[m]] = 1;
  68.            end
  69.            
  70.            adjMatrixRowCounter  = adjMatrixRowCounter + 1;          
  71.            adjMatrix[adjMatrixRowCounter, :] = adjRow;          
  72.        end
  73.     end
  74.     return adjMatrix
  75. end
  76.  
  77. function emptyLabyrinth(nrow, ncol)
  78.    AdjMatrix = generateTableAdjMatrix(nrow, ncol)
  79.    V = Array{Cvor}(undef, nrow*ncol)
  80.    for i in 1:nrow*ncol
  81.      V[i] = Cvor('W', '*', 0, 0)
  82.    end
  83.    return labyrinth(nrow, ncol, AdjMatrix, V)
  84. end
  85.  
  86. function printLabyrinthType(G)
  87.   for i in 1:G.rows
  88.     for j in 1:G.cols
  89.       print(" $(G.V[(i-1)*G.cols+j].type)")
  90.     end
  91.   println()
  92.   end
  93. end
  94.  
  95. function printLabyrinthColor(G)
  96.   for i in 1:G.rows
  97.     for j in 1:G.cols
  98.       print(" $(G.V[(i-1)*G.cols+j].color)")
  99.     end
  100.   println()
  101.   end
  102. end
  103.  
  104. function defineNodesType!(G, nodes, type )
  105.     for u = nodes
  106.         G.V[u].type = type;
  107.     end
  108. end
  109.  
  110. function printLabyrinthPath(G, path)
  111.   for i in 1:G.rows
  112.     for j in 1:G.cols
  113.        if ((i-1)*G.cols+j) in path
  114.          print(" *")
  115.        else
  116.          print(" $(G.V[(i-1)*G.cols+j].type)")
  117.        end
  118.     end
  119.   println()
  120.   end
  121. end
  122.  
  123. #BFS
  124. function BFS!(G, s)
  125.     v = 1:size(G.AdjMatrix, 1)
  126.     for u in v
  127.     if u != s
  128.     G.V[u].color = 'W'
  129.     G.V[u].d = typemax(Int)
  130.     G.V[u].pred = -1
  131.     end
  132.     end
  133.     G.V[s].color = 'G' #gray
  134.     G.V[s].d = 0
  135.     G.V[s].pred = -1
  136.     Q = Int[]
  137.     push!(Q, s)
  138.  
  139.     while ~isempty(Q)
  140.         u = Q[1]
  141.         deleteat!(Q, 1)
  142.         for v in findall(G.AdjMatrix[u,:].==1)
  143.         if G.V[v].color=='W' &&
  144.         G.V[v].type == 'P'
  145.         G.V[v].color = 'G'
  146.         G.V[v].d = G.V[u].d + 1
  147.         G.V[v].pred = u
  148.         push!(Q, v)
  149.         end
  150.         end
  151.         G.V[u].color = 'B'
  152.     end
  153. end
  154.  
  155. function getPath(G, idStart, idEnd)
  156.     path = Int[]
  157.     temp = idEnd;
  158.     while (temp != idStart)
  159.     path = [temp; path]
  160.     temp = G.V[temp].pred;
  161.     end
  162.     return [idStart; path]
  163. end
  164.  
  165. # primer81.jl
  166. # inicjalizujemo podatke u grafu
  167.  
  168. nbRows = 5;
  169. nbColumns = 5;
  170. G = emptyLabyrinth(nbRows, nbColumns)
  171.  
  172. defineNodesType!(G,[1 2 3 5 6 8 10 11 12 13 14 15 17 21 22 23 24 25],'P'); #PUT
  173. defineNodesType!(G, [4 7 9 16 18 19 20], 'Z'); #ZID
  174.  
  175. println("Izgled lavirinta pre obilaska")
  176. println()
  177. printLabyrinthType(G)
  178. println()
  179. println("Ispis lavirinta pre obilaska")
  180. println()
  181. printLabyrinthColor(G)
  182. println()
  183.  
  184. println("Ispis lavirinta posle obilaska")
  185. println()
  186. BFS!(G, 21);
  187. printLabyrinthColor(G)
  188. println()
  189.  
  190. println("Izgled lavirinta sa putanjom")
  191. println()
  192. path = getPath(G, 21, 5)
  193. printLabyrinthPath(G, path)
  194. println()
  195.  
  196. # pr 8.2
  197.  
  198. function BFS2!(G, s)    # sablonski deo
  199.     v = 1:size(G.AdjMatrix, 1)
  200.     for u in v
  201.     if u != s
  202.     G.V[u].color = 'W'
  203.     G.V[u].d = typemax(Int)
  204.     G.V[u].pred = -1
  205.     end
  206. end
  207.     G.V[s].color = 'G'
  208.     G.V[s].d = 0
  209.     G.V[s].pred = -1
  210.     Q = Int[]
  211.     push!(Q, s)
  212.  
  213.     while ~isempty(Q)
  214.         u = Q[1]
  215.         deleteat!(Q, 1)
  216.         if G.V[u].type == 'C'   # odavde krece ne sablonski deo
  217.         path = Int[]
  218.         temp = u;
  219.         while (temp != s)
  220.         path = [temp; path]
  221.         temp = G.V[temp].pred;
  222.         end
  223.         return [s; path]
  224.         else
  225.         for v in findall(G.AdjMatrix[u,:].==1)
  226.         if G.V[v].color=='W' && G.V[v].type!='Z'
  227.         G.V[v].color = 'G'
  228.         G.V[v].d = G.V[u].d + 1
  229.         G.V[v].pred = u
  230.         push!(Q, v)
  231.         end
  232.         end
  233.         end
  234.         G.V[u].color = 'B'
  235.     end
  236.         return Int[];
  237. end
  238.  
  239. nbRows = 8;
  240. nbColumns = 8;
  241. # generisanje lavirinta
  242. G = emptyLabyrinth(nbRows, nbColumns)
  243. # definisanje tipa za odredjeni podskup cvorova iz grafa
  244. defineNodesType!(G, 1:nbRows*nbColumns, 'P');
  245. defineNodesType!(G, [5 9 10 11 13 15 21 23 24 26 28 29 31 34 44 45 46 47 50 55
  246. 58 60 61 63], 'Z');
  247. defineNodesType!(G, [62], 'C');
  248. path = BFS2(G, 57)
  249. # ispis lavirinta
  250. println("Ispis putanje i izgled lavirinta posle obilaska")
  251. println("PATH = $path")
  252. printLabyrinthPath(G, path)
  253.  
  254. # pazi kako indeksiras, ide ovako
  255. # 1 2 3 4 5 6 7 8
  256. # 9 10 11 12 13 14 15 16
  257. # 17 .... za 8x8
  258.  
  259.  
  260. # zadatak 8.1
  261.  
  262. #DFS
  263. function DFS(G, u, k)
  264.     v = 1:size(G.AdjMatrix, 1);
  265.     for u = v
  266.     G.V[u].color = 'W';
  267.     G.V[u].pred = -1;
  268.     end
  269.     for u = v
  270.         if G.V[u].color == 'W';
  271.             return DFS_Visit!(G, u, k);
  272.     end
  273.     end
  274. end
  275.     function DFS_Visit!(G, u, k)
  276.         path =[]
  277.         if u == k
  278.             path=[u]
  279.             return path
  280.         G.V[u].color = 'G';
  281.         for v in findall(G.AdjMatrix[u,:].==1)
  282.         if G.V[v].color=='W' && G.V[v].type != 'Z'
  283.         G.V[v].pred = u;
  284.         path = DFS_Visit!(G, v, k);
  285.         if length(path) != 0
  286.             return [u, path]
  287.         end
  288.         end
  289.         G.V[u].color = 'B';
  290.         return path
  291.     end
  292. #
  293. function getPath(G, idStart, idEnd)
  294.     path = Int[]
  295.     temp = idEnd;
  296.     while (temp != idStart)
  297.     path = [temp; path]
  298.     temp = G.V[temp].pred;
  299.     end
  300.     return [idStart; path]
  301. end
  302.  
  303.  
  304.  
  305. nbRows = 8;
  306. nbColumns = 8;
  307. # generisanje lavirinta
  308. G = emptyLabyrinth(nbRows, nbColumns)
  309. # definisanje tipa za odredjeni podskup cvorova iz grafa
  310. defineNodesType!(G, 1:nbRows*nbColumns, 'P');
  311. defineNodesType!(G, [5 9 10 11 13 15 21 23 24 26 28 29 31 34 44 45 46 47 50 55 58 60 61 63], 'Z');
  312. defineNodesType!(G, [62], 'C');
  313. path = DFS(G, 57)
  314. # ispis lavirinta
  315. println("Ispis putanje i izgled lavirinta posle obilaska")
  316. println("PATH = $path")
  317. printLabyrinthPath(G, path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement