Advertisement
kokusz19

MestInt.KeresesV3.MaybeDone.BeforeCommenting

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