Advertisement
kokusz19

MestInt.Kereses.Done.BeforeRefactoring

May 24th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 22.08 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. /*
  8.  *     1 5 3 4 3 6 7 1 1 6
  9.        4 4 3 4 2 6 2 6 2 5
  10.        1 3 9 4 5 2 4 2 9 5
  11.        5 2 3 5 5 6 4 6 2 4
  12.        1 3 3 2 5 6 5 2 3 2
  13.        2 5 2 5 5 6 4 8 6 1
  14.        9 2 3 6 5 6 2 2 2 *
  15. */
  16.  
  17. namespace Kereses_beadando
  18. {
  19.     class Elem
  20.     {
  21.         public int ertek;
  22.         public int eredmeny;
  23.         public String nyerout;
  24.        
  25.         public Elem(char ertek)
  26.         {
  27.             if (Char.IsDigit(ertek))
  28.                 this.ertek = (int)Char.GetNumericValue(ertek);
  29.             else
  30.                 this.ertek = 0;
  31.             this.eredmeny = 0;
  32.             this.nyerout = "";
  33.         }
  34.     }
  35.  
  36.     class Solution
  37.     {
  38.         public int sorszam;
  39.         public int ertek;
  40.         public int x;
  41.         public int y;
  42.         public String megoldas = "";
  43.  
  44.         public Solution(int x, int y, int sorszam, int ertek, String megoldas)
  45.         {
  46.             this.x = x;
  47.             this.y = y;
  48.             this.sorszam = sorszam;
  49.             this.ertek = ertek;
  50.             this.megoldas = megoldas;
  51.         }
  52.  
  53.     }
  54.  
  55.     class Program
  56.      {
  57.         static void populateArray(char[][] array)
  58.         {
  59.             array[0] = new char[] { '1', '5', '3', '4', '3', '6', '7', '1', '1', '6' };
  60.             array[1] = new char[] { '4', '4', '3', '4', '2', '6', '2', '6', '2', '5' };
  61.             array[2] = new char[] { '1', '3', '9', '4', '5', '2', '4', '2', '9', '5' };
  62.             array[3] = new char[] { '5', '2', '3', '5', '5', '6', '4', '6', '2', '4' };
  63.             array[4] = new char[] { '1', '3', '3', '2', '5', '6', '5', '2', '3', '2' };
  64.             array[5] = new char[] { '2', '5', '2', '5', '5', '6', '4', '8', '6', '1' };
  65.             array[6] = new char[] { '9', '2', '3', '6', '5', '6', '2', '2', '2', '*' };
  66.         }
  67.  
  68.         static void populateTombFromArray(Elem[][] tomb, char[][] array)
  69.         {
  70.             for (int i = 0; i < 7; i++)
  71.             {
  72.                 tomb[i] = new Elem[10];
  73.                 for (int j = 0; j < 10; j++)
  74.                 {
  75.                     tomb[i][j] = new Elem(array[i][j]);
  76.                 }
  77.             }
  78.         }
  79.  
  80.         static void osszesMegoldasa(Elem[][] tomb, char[][] array)
  81.         {
  82.             for (int k = 0; k < 200; k++)
  83.             {
  84.                 int csere = 0;
  85.  
  86.                 for (int i = 9; i >=0 ; i--)
  87.                 {
  88.                     for (int j = 6; j >= 0; j--)
  89.                     {
  90.                         String temp = "";
  91.  
  92.                         if (k == 0 && tomb[j][i].ertek == 0)
  93.                         {
  94.                             tomb[j][i].nyerout = "(" + (j+1) + ", " + (i+1) + ")";
  95.                             tomb[j][i].eredmeny = 1;
  96.                             csere++;
  97.                         }
  98.  
  99.                         else
  100.                         {
  101.                             if (i - tomb[j][i].ertek >= 0)              // Upper row check
  102.                             {
  103.                                 if (tomb[j][i - tomb[j][i].ertek].eredmeny != 0)
  104.                                 {
  105.                                     if (tomb[j][i].nyerout.Equals(""))
  106.                                     {
  107.                                         temp = tomb[j][i - tomb[j][i].ertek].nyerout;
  108.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  109.                                     }
  110.                                     tomb[j][i].eredmeny = 2;
  111.                                     csere++;
  112.                                     goto kilep;
  113.                                 }
  114.                             }
  115.                             if (i - tomb[j][i].ertek >= 0 && j + tomb[j][i].ertek < 7)
  116.                             {
  117.                                 if (tomb[j + tomb[j][i].ertek][i - tomb[j][i].ertek].eredmeny != 0)
  118.                                 {
  119.                                     if (tomb[j][i].nyerout.Equals(""))
  120.                                     {
  121.                                         temp = tomb[j + tomb[j][i].ertek][i - tomb[j][i].ertek].nyerout;
  122.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  123.                                     }
  124.                                     tomb[j][i].eredmeny = 2;
  125.                                     csere++;
  126.                                     goto kilep;
  127.                                 }
  128.                             }
  129.                             if (i - tomb[j][i].ertek >= 0 && j - tomb[j][i].ertek >= 0)
  130.                             {
  131.                                 if (tomb[j - tomb[j][i].ertek][i - tomb[j][i].ertek].eredmeny != 0)
  132.                                 {
  133.                                     if (tomb[j][i].nyerout.Equals(""))
  134.                                     {
  135.                                         temp = tomb[j - tomb[j][i].ertek][i - tomb[j][i].ertek].nyerout;
  136.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  137.                                     }
  138.                                     tomb[j][i].eredmeny = 2;
  139.                                     csere++;
  140.                                     goto kilep;
  141.                                 }
  142.                             }
  143.  
  144.  
  145.                             if (j+tomb[j][i].ertek < 7)                 // Current row check
  146.                             {
  147.                                 if(tomb[j + tomb[j][i].ertek][i].eredmeny != 0)
  148.                                 {
  149.                                     if (tomb[j][i].nyerout.Equals(""))
  150.                                     {
  151.                                         temp = tomb[j + tomb[j][i].ertek][i].nyerout;
  152.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  153.                                     }
  154.                                     tomb[j][i].eredmeny = 2;
  155.                                     csere++;
  156.                                     goto kilep;
  157.                                 }
  158.                             }
  159.                             if (j - tomb[j][i].ertek >= 0)
  160.                             {
  161.                                 if (tomb[j - tomb[j][i].ertek][i].eredmeny != 0)
  162.                                 {
  163.                                     if (tomb[j][i].nyerout.Equals(""))
  164.                                     {
  165.                                         temp = tomb[j - tomb[j][i].ertek][i].nyerout;
  166.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  167.                                     }
  168.                                     tomb[j][i].eredmeny = 2;
  169.                                     csere++;
  170.                                     goto kilep;
  171.                                 }
  172.                             }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.                             if (i + tomb[j][i].ertek < 10)              // Below row check
  180.                             {
  181.                                 if (tomb[j][i + tomb[j][i].ertek].eredmeny != 0)
  182.                                 {
  183.                                     if (tomb[j][i].nyerout.Equals(""))
  184.                                     {
  185.                                         temp = tomb[j][i + tomb[j][i].ertek].nyerout;
  186.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  187.                                     }
  188.                                     tomb[j][i].eredmeny = 2;
  189.                                     csere++;
  190.                                     goto kilep;
  191.                                 }
  192.                             }
  193.                             if (i + tomb[j][i].ertek < 10 && j + tomb[j][i].ertek < 7)
  194.                             {
  195.                                 if (tomb[j + tomb[j][i].ertek][i + tomb[j][i].ertek].eredmeny != 0)
  196.                                 {
  197.                                     if (tomb[j][i].nyerout.Equals(""))
  198.                                     {
  199.                                         temp = tomb[j + tomb[j][i].ertek][i + tomb[j][i].ertek].nyerout;
  200.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  201.                                     }
  202.                                     tomb[j][i].eredmeny = 2;
  203.                                     csere++;
  204.                                     goto kilep;
  205.                                 }
  206.                             }
  207.                             if (i + tomb[j][i].ertek < 10 && j - tomb[j][i].ertek >= 0)
  208.                             {
  209.                                 if (tomb[j - tomb[j][i].ertek][i + tomb[j][i].ertek].eredmeny != 0)
  210.                                 {
  211.                                     if (tomb[j][i].nyerout.Equals(""))
  212.                                     {
  213.                                         temp = tomb[j - tomb[j][i].ertek][i + tomb[j][i].ertek].nyerout;
  214.                                         tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
  215.                                     }
  216.                                     tomb[j][i].eredmeny = 2;
  217.                                     csere++;
  218.                                     goto kilep;
  219.                                 }
  220.                             }
  221.                            
  222.                            
  223.                             kilep: continue;
  224.  
  225.                         }
  226.  
  227.  
  228.                     }
  229.                 }
  230.                 if (csere == 0)
  231.                 {
  232.                     break;
  233.                 }
  234.                 csere = 0;
  235.                 //kiirArray(array);
  236.                 //kiirTomb(tomb);
  237.                 //Console.ReadKey();
  238.  
  239.  
  240.             }
  241.         }
  242.  
  243.         static int[] OrigoKoord(Elem[][] tomb)
  244.         {
  245.             int[] solution = new int[2];
  246.             int a = 0;
  247.             int b = 0;
  248.  
  249.             for (int i = 0; i < 7; i++)
  250.             {
  251.                 for (int j = 0; j < 10; j++)
  252.                 {
  253.                     if(tomb[i][j].ertek == 0)
  254.                     {
  255.                         a = i;
  256.                         b = j;
  257.                     }
  258.                 }
  259.             }
  260.             solution[0] = a;
  261.             solution[1] = b;
  262.             return solution;  
  263.         }
  264.  
  265.         static int tavolsag(int x, int y, int a, int b)
  266.         {
  267.             if (a > x && b > y) {
  268.                 for (int i = 0; i < 10; i++)
  269.                     if (a == x + i && b == y + i)
  270.                         return i;
  271.             }
  272.             if (a > x && b < y) {
  273.                 for (int i = 0; i < 10; i++)
  274.                     if (a == x + i && b + i == y)
  275.                         return i;
  276.             }
  277.             if (a < x && b > y) {
  278.                 for (int i = 0; i < 10; i++)
  279.                     if (a + i == x && b == y + i)
  280.                         return i;
  281.             }
  282.             if (a < x && b < y) {
  283.                 for (int i = 0; i < 10; i++)
  284.                     if (a + i == x && b + i == y)
  285.                         return i;
  286.             }
  287.  
  288.             if (a > x) {
  289.                 for (int i = 0; i < 10; i++)
  290.                     if (a == x + i)
  291.                         return i;
  292.             }
  293.             if (a < x) {
  294.                 for (int i = 0; i < 10; i++)
  295.                     if (a == x + i)
  296.                         return i;
  297.             }
  298.             if (b > y) {
  299.                 for (int i = 0; i < 10; i++)
  300.                     if (b == y + i)
  301.                         return i;
  302.             }
  303.             if (b < y) {
  304.             for (int i = 0; i < 10; i++)
  305.                     if (b + i == y)
  306.                         return i;
  307.             }
  308.  
  309.             return -1;
  310.         }
  311.  
  312.  
  313.         static String keres(Elem[][] tomb, int celx, int cely, List<Solution> lista)
  314.         {
  315.             int max=0;
  316.             for (int k = 0; k < 70; k++)
  317.             {
  318.  
  319.  
  320.  
  321.  
  322.                 max = lista.Count();
  323.  
  324.                 if (k == 0)
  325.                 {
  326.                     lista.Add(new Solution(OrigoKoord(tomb)[0], OrigoKoord(tomb)[1], 1, 0, "(" + (OrigoKoord(tomb)[0] + 1) + ", " + (OrigoKoord(tomb)[1] + 1) + ")"));
  327.                     k++;
  328.                 }
  329.  
  330.  
  331.                 else
  332.                 {
  333.                     for (int j = 0; j <= 70; j++)
  334.                     {
  335.                         for (int l = 1; l <= max; l++)
  336.                         {
  337.                                 Solution item = lista.Where(z => z.sorszam == l).First();
  338.                                 for (int i = 1; i < 10; i++)
  339.                                 {
  340.                                     if ((item.x - i) >= 0 && tomb[item.x - i][item.y].ertek == i)
  341.                                     {
  342.                                         if (lista.Where(x => x.x == (item.x - i) && x.y == item.y).Count() == 0)
  343.                                         {
  344.                                             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));
  345.                                             goto folytat;
  346.                                         }
  347.                                     }
  348.                                     if ((item.x + i) <= OrigoKoord(tomb)[0] && tomb[item.x + i][item.y].ertek == i)
  349.                                     {
  350.                                         if (lista.Where(x => x.x == (item.x + i) && x.y == item.y).Count() == 0)
  351.                                         {
  352.                                             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 ));
  353.                                             goto folytat;
  354.                                         }
  355.                                     }
  356.                                     if ((item.y - i) >= 0 && tomb[item.x][item.y - i].ertek == i)
  357.                                     {
  358.                                         if (lista.Where(x => x.x == item.x && x.y  == (item.y-i)).Count() == 0)
  359.                                         {
  360.                                             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));
  361.                                             goto folytat;
  362.                                         }
  363.                                     }
  364.                                     if ((item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x][item.y+i].ertek == i)
  365.                                     {
  366.                                         if (lista.Where(x => x.x == item.x && x.y == (item.y+ i )).Count() == 0)
  367.                                         {
  368.                                             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));
  369.                                             goto folytat;
  370.                                         }
  371.                                     }
  372.  
  373.  
  374.  
  375.                                     if ((item.x - i) >= 0 && (item.y - i) >= 0 && tomb[item.x - i][item.y-i].ertek == i)
  376.                                     {
  377.                                         if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y - i)).Count() == 0)
  378.                                         {
  379.                                             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));
  380.                                             goto folytat;
  381.                                         }
  382.                                     }
  383.                                     if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x + i][item.y+i].ertek == i)
  384.                                     {
  385.                                         if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y + i)).Count() == 0)
  386.                                         {
  387.                                             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));
  388.                                             goto folytat;
  389.                                         }
  390.                                     }
  391.                                     if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y - i) >= 0 && tomb[item.x + i][item.y - i].ertek == i)
  392.                                     {
  393.                                         if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y - i)).Count() == 0)
  394.                                         {
  395.                                             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));
  396.                                             goto folytat;
  397.                                         }
  398.                                     }
  399.                                     if ((item.x - i >= 0) && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x - i][item.y+i].ertek == i)
  400.                                     {
  401.                                         if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y + i)).Count() == 0)
  402.                                         {
  403.                                             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));
  404.                                             goto folytat;
  405.                                         }
  406.                                     }
  407.                                     else
  408.                                         continue;
  409.                                 }
  410.                             }
  411.                         folytat: continue;
  412.  
  413.                         }
  414.                    
  415.                 }
  416.  
  417.  
  418.             }
  419.             /*
  420.             lista = lista.OrderBy(x => x.y).ThenBy(y=>y.x).ToList();
  421.             foreach (var item in lista)
  422.             {
  423.                 Console.WriteLine((item.x+1) + " " + (item.y+1) + " - " + item.ertek + "\t" + item.megoldas);
  424.             }
  425.             */
  426.             if (lista.Contains(lista.Find(x => x.x == celx && x.y == cely)))
  427.                 return lista.Where(x => x.x == celx && x.y == cely).First().megoldas;
  428.             else
  429.                 return "Nincs megoldas erre a pontra";
  430.         }
  431.  
  432.         /*************************************          KIIRATASOK          ******************************************/
  433.         static void kiirArray(char[][] array)
  434.         {
  435.             for (int i = 0; i < 7; i++)
  436.             {
  437.                 if (i == 0)
  438.                     Console.WriteLine("\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n----------------------------------------------------------------------------------");
  439.                 Console.Write((i + 1) + ". |\t");
  440.                 for (int j = 0; j < 10; j++)
  441.                 {
  442.                     Console.Write(array[i][j] + "\t");
  443.                 }
  444.                 Console.WriteLine();
  445.             }
  446.             Console.WriteLine();
  447.         }
  448.  
  449.         static void kiirVanMegoldas(Elem[][] tomb)
  450.         {
  451.             for (int i = 0; i < 7; i++)
  452.             {
  453.                 Console.Write("\t");
  454.                 for (int j = 0; j < 10; j++)
  455.                 {
  456.                     Console.Write(tomb[i][j].eredmeny + "\t");
  457.                 }
  458.                 Console.WriteLine();
  459.             }
  460.             Console.WriteLine();
  461.         }
  462.  
  463.         static void kiirOsszesMegoldas(Elem[][] tomb)
  464.         {
  465.             for (int j = 0; j < 10; j++)
  466.             {
  467.                 for (int i = 0; i < 7; i++)
  468.                     if (tomb[i][j].eredmeny != 0)
  469.                         Console.WriteLine("Sor: " + (i + 1) + "\t Oszlop: " + (j + 1) + "\tWin: " + tomb[i][j].eredmeny + "\tErtek: " + tomb[i][j].ertek + " Nyero ut: " + tomb[i][j].nyerout);
  470.                 Console.WriteLine();
  471.             }
  472.             Console.WriteLine();
  473.         }
  474.  
  475.         static void kiirAdottKoordinata(Elem[][] tomb, List<Solution> lista)
  476.         {
  477.             Console.WriteLine("\nAdjon meg egy sor, mely egy elemere kivancsi: ");
  478.             int a = Int32.Parse(Console.ReadLine()) - 1;
  479.             Console.WriteLine("Adja meg a sor egy elemét, amelyre kivancsi: ");
  480.             int b = Int32.Parse(Console.ReadLine()) - 1;
  481.            
  482.             Console.WriteLine(keres(tomb, a, b, lista));
  483.         }
  484.  
  485.         /*************************************              MAIN              ******************************************/
  486.         static void Main(string[] args)
  487.         {
  488.  
  489.             List<Solution> lista = new List<Solution>();
  490.  
  491.             char[][] array = new char[7][];
  492.             populateArray(array);
  493.  
  494.             Elem[][] tomb = new Elem[7][];
  495.             populateTombFromArray(tomb, array);
  496.  
  497.             //osszesMegoldasa(tomb, array);
  498.            
  499.  
  500.            
  501.             //kiirVanMegoldas(tomb);
  502.             kiirArray(array);
  503.             kiirAdottKoordinata(tomb, lista);
  504.  
  505.             kiirArray(array);
  506.             //kiirOsszesMegoldas(tomb);
  507.  
  508.             Console.ReadKey();
  509.         }
  510.     }
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement