Advertisement
kokusz19

MestInt.KeresesV2

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