kokusz19

MestInt.Kereses.Done.RefactoredV3

May 25th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Kereses_beadando
  8. {
  9.     class Elem
  10.     {
  11.         public int ertek;
  12.         public int eredmeny;
  13.         public String nyerout;
  14.        
  15.         public Elem(char ertek)
  16.         {
  17.             if (Char.IsDigit(ertek))
  18.                 this.ertek = (int)Char.GetNumericValue(ertek);
  19.             else
  20.                 this.ertek = 0;
  21.             this.eredmeny = 0;
  22.             this.nyerout = "";
  23.         }
  24.     }
  25.  
  26.     class Solution
  27.     {
  28.         public int sorszam;
  29.         public int ertek;
  30.         public int x;
  31.         public int y;
  32.         public String megoldas = "";
  33.  
  34.         public Solution(int x, int y, int sorszam, int ertek, String megoldas)
  35.         {
  36.             this.x = x;
  37.             this.y = y;
  38.             this.sorszam = sorszam;
  39.             this.ertek = ertek;
  40.             this.megoldas = megoldas;
  41.         }
  42.  
  43.     }
  44.  
  45.     class Program
  46.      {
  47.  
  48.         /*************************************           OTHERS            ******************************************/
  49.         static int[] OrigoKoord(Elem[][] tomb)
  50.         {
  51.             int[] solution = new int[2];
  52.             int a = 0;
  53.             int b = 0;
  54.  
  55.             for (int i = 0; i < 7; i++)
  56.             {
  57.                 for (int j = 0; j < 10; j++)
  58.                 {
  59.                     if(tomb[i][j].ertek == 0)
  60.                     {
  61.                         a = i;
  62.                         b = j;
  63.                     }
  64.                 }
  65.             }
  66.             solution[0] = a;
  67.             solution[1] = b;
  68.             return solution;  
  69.         }
  70.  
  71.         static String keres(Elem[][] tomb, int celx, int cely, List<Solution> lista)
  72.         {
  73.             int max=0, change = 0;
  74.             for (int k = 0; k < 70; k++)
  75.             {
  76.                 max = lista.Count();
  77.  
  78.                 if (lista.Contains(lista.Find(x => x.x == celx && x.y == cely)))
  79.                     break;
  80.  
  81.                 if (k == 0)
  82.                 {
  83.                     lista.Add(new Solution(OrigoKoord(tomb)[0], OrigoKoord(tomb)[1], 1, 0, "(" + (OrigoKoord(tomb)[0] + 1) + ", " + (OrigoKoord(tomb)[1] + 1) + ")"));
  84.                     k++;
  85.                 }
  86.  
  87.  
  88.                 else
  89.                 {
  90.                     for (int j = 0; j <= 70; j++)
  91.                     {
  92.                         for (int l = 1; l <= max; l++)
  93.                         {
  94.                             Solution item = lista.Where(z => z.sorszam == l).First();
  95.                             for (int i = 1; i < 10; i++)
  96.                             {
  97.                                 if ((item.x - i) >= 0 && tomb[item.x - i][item.y].ertek == i)
  98.                                 {
  99.                                     if (lista.Where(x => x.x == (item.x - i) && x.y == item.y).Count() == 0)
  100.                                     {
  101.                                         lista.Add(new Solution((item.x - i), item.y, (lista.Max(x => x.sorszam) + 1), tomb[item.x - i][item.y].ertek, "(" + (item.x - i + 1) + ", " + (item.y + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  102.                                         //goto folytat;
  103.                                     }
  104.                                 }
  105.                                 if ((item.x + i) <= OrigoKoord(tomb)[0] && tomb[item.x + i][item.y].ertek == i)
  106.                                 {
  107.                                     if (lista.Where(x => x.x == (item.x + i) && x.y == item.y).Count() == 0)
  108.                                     {
  109.                                         lista.Add(new Solution((item.x + i), item.y, (lista.Max(x => x.sorszam) + 1), tomb[item.x + i][item.y].ertek, "(" + (item.x + i + 1) + ", " + (item.y + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  110.                                         //goto folytat;
  111.                                     }
  112.                                 }
  113.                                 if ((item.y - i) >= 0 && tomb[item.x][item.y - i].ertek == i)
  114.                                 {
  115.                                     if (lista.Where(x => x.x == item.x && x.y == (item.y - i)).Count() == 0)
  116.                                     {
  117.                                         lista.Add(new Solution((item.x), (item.y - i), (lista.Max(x => x.sorszam) + 1), tomb[item.x][item.y - i].ertek, "(" + (item.x + 1) + ", " + (item.y - i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  118.                                         //goto folytat;
  119.                                     }
  120.                                 }
  121.                                 if ((item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x][item.y + i].ertek == i)
  122.                                 {
  123.                                     if (lista.Where(x => x.x == item.x && x.y == (item.y + i)).Count() == 0)
  124.                                     {
  125.                                         lista.Add(new Solution((item.x), item.y + i, (lista.Max(x => x.sorszam) + 1), tomb[item.x][item.y + i].ertek, "(" + (item.x + 1) + ", " + (item.y + i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  126.                                         //goto folytat;
  127.                                     }
  128.                                 }
  129.  
  130.  
  131.  
  132.                                 if ((item.x - i) >= 0 && (item.y - i) >= 0 && tomb[item.x - i][item.y - i].ertek == i)
  133.                                 {
  134.                                     if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y - i)).Count() == 0)
  135.                                     {
  136.                                         lista.Add(new Solution((item.x - i), (item.y - i), (lista.Max(x => x.sorszam) + 1), tomb[item.x - i][item.y - i].ertek, "(" + (item.x - i + 1) + ", " + (item.y - i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  137.                                         //goto folytat;
  138.                                     }
  139.                                 }
  140.                                 if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x + i][item.y + i].ertek == i)
  141.                                 {
  142.                                     if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y + i)).Count() == 0)
  143.                                     {
  144.                                         lista.Add(new Solution((item.x + i), (item.y + i), (lista.Max(x => x.sorszam) + 1), tomb[item.x + i][item.y + i].ertek, "(" + (item.x + i + 1) + ", " + (item.y + i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  145.                                         //goto folytat;
  146.                                     }
  147.                                 }
  148.                                 if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y - i) >= 0 && tomb[item.x + i][item.y - i].ertek == i)
  149.                                 {
  150.                                     if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y - i)).Count() == 0)
  151.                                     {
  152.                                         lista.Add(new Solution((item.x + i), (item.y - i), (lista.Max(x => x.sorszam) + 1), tomb[item.x + i][item.y - i].ertek, "(" + (item.x + i + 1) + ", " + (item.y - i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  153.                                         //goto folytat;
  154.                                     }
  155.                                 }
  156.                                 if ((item.x - i >= 0) && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x - i][item.y + i].ertek == i)
  157.                                 {
  158.                                     if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y + i)).Count() == 0)
  159.                                     {
  160.                                         lista.Add(new Solution((item.x - i), (item.y + i), (lista.Max(x => x.sorszam) + 1), tomb[item.x - i][item.y + i].ertek, "(" + (item.x - i + 1) + ", " + (item.y + i + 1) + ") -> " + lista.Where(x => x.x == item.x && x.y == item.y).First().megoldas));
  161.                                         //goto folytat;
  162.                                     }
  163.                                 }
  164.                                 if (max != lista.Count)
  165.                                     change = 1;
  166.                                 else
  167.                                     continue;
  168.                             }
  169.                         }
  170.                         if (change == 0)
  171.                             break;
  172.                         else
  173.                             change = 0;
  174.                     //folytat: continue;
  175.                     }
  176.                 }
  177.             }
  178.  
  179.             if (lista.Contains(lista.Find(x => x.x == celx && x.y == cely)))
  180.                 return lista.Where(x => x.x == celx && x.y == cely).First().megoldas;
  181.             else
  182.                 return "Nincs megoldas erre a pontra";
  183.         }
  184.  
  185.         /*************************************      TOMB FELTOLTESEK        *******************************************/
  186.         static void populateArray(char[][] array)
  187.         {
  188.             array[0] = new char[] { '1', '5', '3', '4', '3', '6', '7', '1', '1', '6' };
  189.             array[1] = new char[] { '4', '4', '3', '4', '2', '6', '2', '6', '2', '5' };
  190.             array[2] = new char[] { '1', '3', '9', '4', '5', '2', '4', '2', '9', '5' };
  191.             array[3] = new char[] { '5', '2', '3', '5', '5', '6', '4', '6', '2', '4' };
  192.             array[4] = new char[] { '1', '3', '3', '2', '5', '6', '5', '2', '3', '2' };
  193.             array[5] = new char[] { '2', '5', '2', '5', '5', '6', '4', '8', '6', '1' };
  194.             array[6] = new char[] { '9', '2', '3', '6', '5', '6', '2', '2', '2', '*' };
  195.         }
  196.  
  197.         static void populateTombFromArray(Elem[][] tomb, char[][] array)
  198.         {
  199.             for (int i = 0; i < 7; i++)
  200.             {
  201.                 tomb[i] = new Elem[10];
  202.                 for (int j = 0; j < 10; j++)
  203.                 {
  204.                     tomb[i][j] = new Elem(array[i][j]);
  205.                 }
  206.             }
  207.         }
  208.  
  209.         /*************************************          KIIRATASOK          *******************************************/
  210.         static void kiirArray(char[][] array)
  211.         {
  212.             for (int i = 0; i < 7; i++)
  213.             {
  214.                 if (i == 0)
  215.                     Console.WriteLine("\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n----------------------------------------------------------------------------------");
  216.                 Console.Write((i + 1) + ". |\t");
  217.                 for (int j = 0; j < 10; j++)
  218.                 {
  219.                     Console.Write(array[i][j] + "\t");
  220.                 }
  221.                 Console.WriteLine();
  222.             }
  223.             Console.WriteLine();
  224.         }
  225.  
  226.         static void kiirAdottKoordinata(Elem[][] tomb, List<Solution> lista)
  227.         {
  228.             Console.WriteLine("\nAdjon meg egy sor, mely egy elemere kivancsi: ");
  229.             int a = Int32.Parse(Console.ReadLine()) - 1;
  230.             Console.WriteLine("Adja meg a sor egy elemét, amelyre kivancsi: ");
  231.             int b = Int32.Parse(Console.ReadLine()) - 1;
  232.            
  233.             Console.WriteLine(keres(tomb, a, b, lista));
  234.         }
  235.  
  236.         static void kiirMegoldhato(List<Solution> lista)
  237.         {
  238.             Console.WriteLine("Megoldhato koordinatak (ha hamarabb megtalalja a keresett elemet, akkor a vizsgalat nem fut vegig): ");
  239.             for (int i = 0; i < 7; i++)
  240.             {
  241.                 if (i == 0)
  242.                     Console.WriteLine("\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n----------------------------------------------------------------------------------");
  243.                 Console.Write((i + 1) + ". |\t");
  244.                 for (int j = 0; j < 10; j++)
  245.                 {
  246.                     if (lista.Contains(lista.Find(x => x.x == i && x.y == j)))
  247.                         Console.Write(lista.Where(x => x.x == i && x.y == j).First().ertek + "\t");
  248.                     else
  249.                         Console.Write("-\t");
  250.                 }
  251.                 Console.WriteLine();
  252.             }
  253.         }
  254.  
  255.         /*************************************              MAIN              ******************************************/
  256.         static void Main(string[] args)
  257.         {
  258.  
  259.             List<Solution> lista = new List<Solution>();
  260.  
  261.             char[][] array = new char[7][];
  262.             populateArray(array);
  263.  
  264.             Elem[][] tomb = new Elem[7][];
  265.             populateTombFromArray(tomb, array);
  266.  
  267.             kiirArray(array);
  268.  
  269.             kiirAdottKoordinata(tomb, lista);
  270.  
  271.             kiirArray(array);
  272.             kiirMegoldhato(lista);
  273.  
  274.             Console.ReadKey();
  275.         }
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment