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 Kereses_beadando
- {
- class Elem
- {
- public int ertek;
- public int eredmeny;
- public String nyerout;
- public Elem(char ertek)
- {
- if (Char.IsDigit(ertek))
- this.ertek = (int)Char.GetNumericValue(ertek);
- else
- this.ertek = 0;
- this.eredmeny = 0;
- this.nyerout = "";
- }
- }
- class Solution
- {
- public int sorszam;
- public int ertek;
- public int x;
- public int y;
- public String megoldas = "";
- public Solution(int x, int y, int sorszam, int ertek, String megoldas)
- {
- this.x = x;
- this.y = y;
- this.sorszam = sorszam;
- this.ertek = ertek;
- this.megoldas = megoldas;
- }
- }
- class Program
- {
- /************************************* OTHERS ******************************************/
- static int[] OrigoKoord(Elem[][] tomb)
- {
- int[] solution = new int[2];
- int a = 0;
- int b = 0;
- for (int i = 0; i < 7; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if(tomb[i][j].ertek == 0)
- {
- a = i;
- b = j;
- }
- }
- }
- solution[0] = a;
- solution[1] = b;
- return solution;
- }
- static String keres(Elem[][] tomb, int celx, int cely, List<Solution> lista)
- {
- int max=0;
- for (int k = 0; k < 70; k++)
- {
- max = lista.Count();
- if (k == 0)
- {
- lista.Add(new Solution(OrigoKoord(tomb)[0], OrigoKoord(tomb)[1], 1, 0, "(" + (OrigoKoord(tomb)[0] + 1) + ", " + (OrigoKoord(tomb)[1] + 1) + ")"));
- k++;
- }
- else
- {
- for (int j = 0; j <= 70; j++)
- {
- for (int l = 1; l <= max; l++)
- {
- Solution item = lista.Where(z => z.sorszam == l).First();
- for (int i = 1; i < 10; i++)
- {
- if ((item.x - i) >= 0 && tomb[item.x - i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x - i) && x.y == item.y).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.x + i) <= OrigoKoord(tomb)[0] && tomb[item.x + i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x + i) && x.y == item.y).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.y - i) >= 0 && tomb[item.x][item.y - i].ertek == i)
- {
- if (lista.Where(x => x.x == item.x && x.y == (item.y - i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x][item.y + i].ertek == i)
- {
- if (lista.Where(x => x.x == item.x && x.y == (item.y + i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.x - i) >= 0 && (item.y - i) >= 0 && tomb[item.x - i][item.y - i].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y - i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x + i][item.y + i].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y + i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.x + i) <= OrigoKoord(tomb)[0] && (item.y - i) >= 0 && tomb[item.x + i][item.y - i].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x + i) && (x.y == item.y - i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- if ((item.x - i >= 0) && (item.y + i) <= OrigoKoord(tomb)[1] && tomb[item.x - i][item.y + i].ertek == i)
- {
- if (lista.Where(x => x.x == (item.x - i) && x.y == (item.y + i)).Count() == 0)
- {
- 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));
- goto folytat;
- }
- }
- else
- continue;
- }
- }
- folytat: continue;
- }
- }
- }
- if (lista.Contains(lista.Find(x => x.x == celx && x.y == cely)))
- return lista.Where(x => x.x == celx && x.y == cely).First().megoldas;
- else
- return "Nincs megoldas erre a pontra";
- }
- /************************************* TOMB FELTOLTESEK *******************************************/
- static void populateArray(char[][] array)
- {
- array[0] = new char[] { '1', '5', '3', '4', '3', '6', '7', '1', '1', '6' };
- array[1] = new char[] { '4', '4', '3', '4', '2', '6', '2', '6', '2', '5' };
- array[2] = new char[] { '1', '3', '9', '4', '5', '2', '4', '2', '9', '5' };
- array[3] = new char[] { '5', '2', '3', '5', '5', '6', '4', '6', '2', '4' };
- array[4] = new char[] { '1', '3', '3', '2', '5', '6', '5', '2', '3', '2' };
- array[5] = new char[] { '2', '5', '2', '5', '5', '6', '4', '8', '6', '1' };
- array[6] = new char[] { '9', '2', '3', '6', '5', '6', '2', '2', '2', '*' };
- }
- static void populateTombFromArray(Elem[][] tomb, char[][] array)
- {
- for (int i = 0; i < 7; i++)
- {
- tomb[i] = new Elem[10];
- for (int j = 0; j < 10; j++)
- {
- tomb[i][j] = new Elem(array[i][j]);
- }
- }
- }
- /************************************* KIIRATASOK *******************************************/
- static void kiirArray(char[][] array)
- {
- for (int i = 0; i < 7; i++)
- {
- if (i == 0)
- Console.WriteLine("\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n----------------------------------------------------------------------------------");
- Console.Write((i + 1) + ". |\t");
- for (int j = 0; j < 10; j++)
- {
- Console.Write(array[i][j] + "\t");
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- static void kiirAdottKoordinata(Elem[][] tomb, List<Solution> lista)
- {
- Console.WriteLine("\nAdjon meg egy sor, mely egy elemere kivancsi: ");
- int a = Int32.Parse(Console.ReadLine()) - 1;
- Console.WriteLine("Adja meg a sor egy elemét, amelyre kivancsi: ");
- int b = Int32.Parse(Console.ReadLine()) - 1;
- Console.WriteLine(keres(tomb, a, b, lista));
- }
- static void kiirMegoldhato(List<Solution> lista)
- {
- Console.WriteLine("Megoldhato koordinatak: ");
- for (int i = 0; i < 7; i++)
- {
- if (i == 0)
- Console.WriteLine("\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n----------------------------------------------------------------------------------");
- Console.Write((i + 1) + ". |\t");
- for (int j = 0; j < 10; j++)
- {
- if (lista.Contains(lista.Find(x => x.x == i && x.y == j)))
- Console.Write(lista.Where(x => x.x == i && x.y == j).First().ertek + "\t");
- else
- Console.Write("-\t");
- }
- Console.WriteLine();
- }
- }
- /************************************* MAIN ******************************************/
- static void Main(string[] args)
- {
- List<Solution> lista = new List<Solution>();
- char[][] array = new char[7][];
- populateArray(array);
- Elem[][] tomb = new Elem[7][];
- populateTombFromArray(tomb, array);
- kiirArray(array);
- kiirAdottKoordinata(tomb, lista);
- kiirArray(array);
- kiirMegoldhato(lista);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement