+ 2

C# BFS

Okey, so I tried to write dfs in c# for my small game in unity and everything seems to be okey, but it doesn't work!! I got some really strange mistakes and I got confused. Looking for some help and explanatin)). I will appreciate any comments. Here is the code: List<Vector2> returnWayTo(int pos, int goal) { Queue<int> q = new Queue<int>(); List<bool> used = new List<bool>(28); List<int> d = new List<int>(28); List<int> p = new List<int>(28); q.Enqueue(pos); used[pos] = true; p[pos] = -1; while(q.Count != 0) { int v = q.Dequeue(); for (int i = 0; i < g[v].Count; i++) { int to = g[v][i]; if (!used[to]) { used[to] = true; q.Enqueue(to); d[to] = d[v] + 1; p[to] = v; } } } List<Vector2> path = new List<Vector2>(); for (int v = goal; v != -1; v = p[v]) { path.Add(new Vector2(CountFrom.x + size * (v % lenth), CountFrom.y + size * (v / lenth))); } path.Reverse(); return path; } -> And when I try to use this function with any data, I receive a comment that mistake is with used[pos], but it's impossible. *Of course I have list<list<int>> g before the function, it's only a part of the code.

28th May 2018, 8:07 AM
OlegBezr
OlegBezr - avatar
1 Resposta
+ 1
Where is used defined. I think this is one of those things where visualisation is key. I don't exactly know what you are trying to achieve besides finding the first solution to reach another point.
28th May 2018, 8:22 PM
Nommer101
Nommer101 - avatar