Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ketszemelyesV2
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Csucs> lista = new List<Csucs>();
- treeSetup(lista);
- kiirFullLista(lista);
- jatekfa.Add(new Jatekfa(0, 1, lista));
- megoldasok(jatekfa);
- jatekfakkiiratasa(jatekfa);
- Console.ReadKey();
- }
- static void treeSetup(List<Csucs> lista)
- {
- /*
- * A
- * B C
- * D E F G H
- * I J K L
- *
- */
- lista.Add(new Csucs(0, 1, "A"));
- lista.Add(new Csucs(1, 2, "B"));
- lista.Add(new Csucs(1, 3, "C"));
- lista.Add(new Csucs(2, 4, "D"));
- lista.Add(new Csucs(2, 5, "E"));
- lista.Add(new Csucs(2, 6, "F"));
- lista.Add(new Csucs(3, 7, "G"));
- lista.Add(new Csucs(3, 8, "H"));
- lista.Add(new Csucs(4, 9, "I"));
- lista.Add(new Csucs(4, 10, "J"));
- lista.Add(new Csucs(6, 11, "K"));
- lista.Add(new Csucs(8, 12, "L"));
- /**/
- foreach (Csucs item in lista)
- {
- foreach (Csucs item2 in lista)
- {
- if (item.sorszam != item2.sorszam && item.sorszam == item2.szulosorszam)
- {
- item.gyerekekInt.Add(item2.sorszam);
- item.gyerekekString.Add(lista.Where(x => x.sorszam == item2.sorszam).First().nev);
- }
- }
- item.letezik = 1;
- }
- }
- static void kiirFullLista(List<Csucs> lista)
- {
- Console.WriteLine("###############\t Lista\t##################");
- foreach (Csucs item in lista)
- {
- if (item.letezik == 1)
- {
- Console.Write(item.sorszam + ".\t" + item.nev + "\t(szulo: " + item.szulosorszam + " gyerekei:");
- if (item.gyerekekInt.Count != 0)
- {
- foreach (int item2 in item.gyerekekInt)
- {
- Console.Write(" " + item2 + "(" + lista.Find(x => x.sorszam == item2).nev + ")");
- }
- }
- else
- Console.Write(" level elem");
- Console.WriteLine(")");
- }
- }
- Console.WriteLine();
- }
- public static List<Jatekfa> jatekfa = new List<Jatekfa>();
- static void megoldasok(List<Jatekfa> jatekfak)
- {
- if(jatekfa.Where(x=>x.kiterjesztve == 0).Count() != 0)
- {
- List<Jatekfa> localjatekfa = new List<Jatekfa>();
- foreach (Jatekfa item in jatekfak)
- localjatekfa.Add(new Jatekfa(item.szulo, item.sorszam, item.adottFaAllapot, item.kiterjesztve));
- // A még ki nem terjesztett fa állapotokat veszi végig
- foreach (var jatekfaa in localjatekfa.Where(x=>x.kiterjesztve == 0))
- {
- // Ha a fából lehet törölni
- int szulo = jatekfaa.sorszam;
- if (jatekfaa.adottFaAllapot.Where(x => x.letezik != 0).Count() > 1)
- {
- for (int i = 1; i <= jatekfaa.adottFaAllapot.Count(); i++)
- {
- // lokalis lista letrehozasa a fa adott allapotarol
- List<Csucs> listacopy = new List<Csucs>();
- foreach (var csucs in jatekfaa.adottFaAllapot)
- listacopy.Add(new Csucs(csucs.szulosorszam, csucs.sorszam, csucs.nev, csucs.letezik, csucs.gyerekekInt, csucs.gyerekekString));
- int sorszam = i;
- if (listacopy.Where(x => x.sorszam == sorszam && x.letezik == 1 && x.gyerekekInt.Count() == 0).Count() == 0)
- continue;
- // else if (listacopy.Find(x => x.sorszam == sorszam).letezik != 1 || listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Count == 0)
- // continue;
- // ha törölni lehet
- else
- {
- Csucs csucsszuloje = listacopy.Find(x => x.sorszam == sorszam);
- // unoka.szulosorszam updateje
- // unokak kigyujtese a gyerekek listaba
- List<int> gyerekek = new List<int>();
- foreach (Csucs gyerek in listacopy.Where(x => x.szulosorszam == sorszam))
- foreach (int unoka in gyerek.gyerekekInt)
- gyerekek.Add(unoka);
- // unoka szulojenek atirasa a nagyszulojere
- foreach (Csucs item in listacopy)
- if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
- item.szulosorszam = sorszam;
- // szülő gyerekeinek logikai törlése
- foreach (int gyereke in csucsszuloje.gyerekekInt)
- {
- foreach (var csucs in listacopy)
- {
- if (csucs.sorszam == gyereke)
- {
- listacopy.Find(x => x == csucs).letezik = 0;
- listacopy.Find(x => x == csucs).gyerekekInt.Clear();
- listacopy.Find(x => x == csucs).gyerekekString.Clear();
- }
- }
- }
- // szülő gyerekeinek updateje
- listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
- listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
- foreach (Csucs item in listacopy)
- {
- if (item.szulosorszam == sorszam && item.letezik == 1)
- {
- listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
- listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
- }
- }
- jatekfa.Add(new Jatekfa(szulo, jatekfak.Count() + 1, listacopy));
- }
- }
- // jatekfa.Find(x => x.sorszam == szulo).kiterjesztve = 1;
- }
- // Ha a fából nem lehet törölni, akkor a csucs ki van terjesztve, nem lehet vele mit csinalni
- //else
- jatekfa.Find(x => x.sorszam == szulo).kiterjesztve = 1;
- }
- }
- }
- static void jatekfakkiiratasa(List<Jatekfa> jatekfak)
- {
- Console.WriteLine("\nJatekfak:");
- foreach (var jatekfa in jatekfak)
- {
- Console.Write(jatekfa.sorszam + ".\t(szulo: " + jatekfa.szulo + ")\t");
- foreach (var csucs in jatekfa.adottFaAllapot.Where(x=>x.letezik != 0))
- {
- Console.Write(" " + csucs.nev + "(" + csucs.sorszam + "." + (csucs.letezik == 1 ? ")" : " nem letezik)"));
- }
- Console.WriteLine("\n");
- }
- }
- }
- class Csucs
- {
- public int szulosorszam;
- public int sorszam;
- public String nev;
- public int letezik;
- public List<int> gyerekekInt = new List<int>();
- public List<String> gyerekekString = new List<string>();
- public Csucs(int szulosorszam, int sorszam, String nev)
- {
- this.szulosorszam = szulosorszam;
- this.sorszam = sorszam;
- this.nev = nev;
- this.letezik = 1;
- }
- public Csucs(int szulosorszam, int sorszam, String nev, int letezik, List<int> gyerekekInt, List<String> gyerekekString)
- {
- this.szulosorszam = szulosorszam;
- this.sorszam = sorszam;
- this.nev = nev;
- this.letezik = letezik;
- this.gyerekekInt = gyerekekInt.ToList();
- this.gyerekekString = gyerekekString.ToList();
- }
- }
- class Jatekfa
- {
- public int szulo;
- //public int win; Később kiszámolható
- public int sorszam;
- public List<Csucs> adottFaAllapot;
- public int kiterjesztve;
- public Jatekfa(int szulo, /* int win, */int sorszam, List<Csucs> adottFaAllapot)
- {
- this.szulo = szulo;
- //this.win = win;
- this.sorszam = sorszam;
- this.adottFaAllapot = adottFaAllapot;
- this.kiterjesztve = 0;
- }
- public Jatekfa(int szulo, /* int win, */int sorszam, List<Csucs> adottFaAllapot, int kiterjesztve)
- {
- this.szulo = szulo;
- //this.win = win;
- this.sorszam = sorszam;
- this.adottFaAllapot = adottFaAllapot;
- this.kiterjesztve = kiterjesztve;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement