Skip to content

Instantly share code, notes, and snippets.

@antcolag
Last active January 29, 2021 10:54
Show Gist options
  • Save antcolag/d0b9da0aef570f9aa69fe2aec8159004 to your computer and use it in GitHub Desktop.
Save antcolag/d0b9da0aef570f9aa69fe2aec8159004 to your computer and use it in GitHub Desktop.
l'algoritmo della ricerca dei cammini più giusti sugli alberi
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;
}
}
@antcolag
Copy link
Author

antcolag commented Jan 29, 2021

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

  • figlio1
    • figlio 2
    • figlio 3
      • figlio 4
    • figlio 5
  • figlio 6
    • figlio 7

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment