Last active
January 29, 2021 10:54
-
-
Save antcolag/d0b9da0aef570f9aa69fe2aec8159004 to your computer and use it in GitHub Desktop.
l'algoritmo della ricerca dei cammini più giusti sugli alberi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductShotsPattern | |
{ | |
public string Name { get; set; } | |
public int Data { get; set; } | |
public List<string> Pattern { get; set; } | |
public List<ProductShotsPattern> Overrides { get; set; } | |
public ProductShotsPattern Find(string search, int depth = 0) | |
{ | |
if (string.IsNullOrEmpty(search)) | |
{ | |
return null; | |
} | |
_depth = depth; | |
var path = new Queue<string>(search.Split(' ')); | |
var next = path.Peek(); | |
_path = ""; | |
if (next.Equals("!" + Name)) | |
{ | |
return null; | |
} | |
if (next.Split('|').Any(val => val == Name)) | |
{ | |
path.Dequeue(); | |
_path = Name; | |
if (Pattern != null && Pattern.Count > 0) | |
{ | |
_depth++; | |
} | |
} | |
if (Overrides?.Count > 0) | |
{ | |
ProductShotsPattern max = this; | |
Overrides.Select(conf => conf.Find(string.Join(" ", path), _depth)).ForEach(item => | |
{ | |
max = item?._depth > max._depth ? item : max; | |
}); | |
if (max != this) | |
{ | |
if (!string.IsNullOrEmpty(_path)) | |
{ | |
max._path = _path + " " + max._path; | |
} | |
if (Pattern == null || Pattern.Count == 0) | |
{ | |
max._depth++; | |
} | |
} | |
return max; | |
} | |
return this; | |
} | |
private int _depth; | |
private string _path; | |
public string GetPath() | |
{ | |
return _path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
non ricordo bene come funzionava la questione... ma c'era un json che veniva attraversato per stampare delle immagini....
l'idea è che questa classe rappresenta un nodo in un albero e la funzione find trova il nodo che più si avvicina al path che gli hai dato in pasto
tipo
root
root.find("figlio1 figlio3 figlio 8) restituisce figlio3 perché è quello che si avvicina di più nel path figlio1->figlio3->figlio8, dal momento che figlio8 non esiste