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;
- /*
- * 1 5 3 4 3 6 7 1 1 6
- 4 4 3 4 2 6 2 6 2 5
- 1 3 9 4 5 2 4 2 9 5
- 5 2 3 5 5 6 4 6 2 4
- 1 3 3 2 5 6 5 2 3 2
- 2 5 2 5 5 6 4 8 6 1
- 9 2 3 6 5 6 2 2 2 *
- */
- 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)
- {
- this.x = x;
- this.y = y;
- this.sorszam = sorszam;
- this.ertek = ertek;
- }
- }
- class Program
- {
- 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]);
- }
- }
- }
- static void osszesMegoldasa(Elem[][] tomb, char[][] array)
- {
- for (int k = 0; k < 200; k++)
- {
- int csere = 0;
- for (int i = 9; i >=0 ; i--)
- {
- for (int j = 6; j >= 0; j--)
- {
- String temp = "";
- if (k == 0 && tomb[j][i].ertek == 0)
- {
- tomb[j][i].nyerout = "(" + (j+1) + ", " + (i+1) + ")";
- tomb[j][i].eredmeny = 1;
- csere++;
- }
- else
- {
- if (i - tomb[j][i].ertek >= 0) // Upper row check
- {
- if (tomb[j][i - tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j][i - tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (i - tomb[j][i].ertek >= 0 && j + tomb[j][i].ertek < 7)
- {
- if (tomb[j + tomb[j][i].ertek][i - tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j + tomb[j][i].ertek][i - tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (i - tomb[j][i].ertek >= 0 && j - tomb[j][i].ertek >= 0)
- {
- if (tomb[j - tomb[j][i].ertek][i - tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j - tomb[j][i].ertek][i - tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (j+tomb[j][i].ertek < 7) // Current row check
- {
- if(tomb[j + tomb[j][i].ertek][i].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j + tomb[j][i].ertek][i].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (j - tomb[j][i].ertek >= 0)
- {
- if (tomb[j - tomb[j][i].ertek][i].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j - tomb[j][i].ertek][i].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (i + tomb[j][i].ertek < 10) // Below row check
- {
- if (tomb[j][i + tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j][i + tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (i + tomb[j][i].ertek < 10 && j + tomb[j][i].ertek < 7)
- {
- if (tomb[j + tomb[j][i].ertek][i + tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j + tomb[j][i].ertek][i + tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- if (i + tomb[j][i].ertek < 10 && j - tomb[j][i].ertek >= 0)
- {
- if (tomb[j - tomb[j][i].ertek][i + tomb[j][i].ertek].eredmeny != 0)
- {
- if (tomb[j][i].nyerout.Equals(""))
- {
- temp = tomb[j - tomb[j][i].ertek][i + tomb[j][i].ertek].nyerout;
- tomb[j][i].nyerout = "(" + (j + 1) + ", " + (i + 1) + ") -> " + temp;
- }
- tomb[j][i].eredmeny = 2;
- csere++;
- goto kilep;
- }
- }
- kilep: continue;
- }
- }
- }
- if (csere == 0)
- {
- break;
- }
- csere = 0;
- //kiirArray(array);
- //kiirTomb(tomb);
- //Console.ReadKey();
- }
- }
- 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 int tavolsag(int x, int y, int a, int b)
- {
- if (a > x && b > y) {
- for (int i = 0; i < 10; i++)
- if (a == x + i && b == y + i)
- return i;
- }
- if (a > x && b < y) {
- for (int i = 0; i < 10; i++)
- if (a == x + i && b + i == y)
- return i;
- }
- if (a < x && b > y) {
- for (int i = 0; i < 10; i++)
- if (a + i == x && b == y + i)
- return i;
- }
- if (a < x && b < y) {
- for (int i = 0; i < 10; i++)
- if (a + i == x && b + i == y)
- return i;
- }
- if (a > x) {
- for (int i = 0; i < 10; i++)
- if (a == x + i)
- return i;
- }
- if (a < x) {
- for (int i = 0; i < 10; i++)
- if (a == x + i)
- return i;
- }
- if (b > y) {
- for (int i = 0; i < 10; i++)
- if (b == y + i)
- return i;
- }
- if (b < y) {
- for (int i = 0; i < 10; i++)
- if (b + i == y)
- return i;
- }
- return -1;
- }
- static String keres(Elem[][] tomb, int celx, int cely, List<Solution> lista)
- {
- for (int k = 0; k < 70; k++)
- {
- if (k == 0)
- {
- lista.Add(new Solution(OrigoKoord(tomb)[0], OrigoKoord(tomb)[1], 1, 0));
- lista.Where(x => x.sorszam == 1).First().megoldas = "(" + (OrigoKoord(tomb)[0]+1) + ", " + (OrigoKoord(tomb)[1]+1) + ")";
- }
- else
- {
- for (int j = 0; j <= 70; j++)
- {
- foreach (var item in lista)
- {
- 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));
- 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));
- 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 - i == item.y).Count() == 0)
- {
- lista.Add(new Solution(item.x, item.y - i, lista.Max(x => x.sorszam) + 1, tomb[item.x][item.y - i].ertek));
- goto folytat;
- }
- }
- if (item.y + i <= OrigoKoord(tomb)[1] && tomb[item.x - i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == item.x && x.y + i== item.y).Count() == 0)
- {
- lista.Add(new Solution(item.x, item.y + i, lista.Max(x => x.sorszam) + 1, tomb[item.x][item.y + i].ertek));
- goto folytat;
- }
- }
- if (item.x - i >= 0 && item.y - i >= 0 && tomb[item.x - i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == item.x - i && x.y - i == item.y).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));
- goto folytat;
- }
- }
- if (item.x + i <= OrigoKoord(tomb)[0] && item.y + i <= OrigoKoord(tomb)[1] && tomb[item.x + i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == item.x + i && x.y + i == item.y).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));
- goto folytat;
- }
- }
- if (item.x + i <= OrigoKoord(tomb)[0] && item.y - i >= 0 && tomb[item.x - i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == item.x + i && x.y - i == item.y).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));
- goto folytat;
- }
- }
- if (item.x - i >= 0 && item.y + i <= OrigoKoord(tomb)[1] && tomb[item.x - i][item.y].ertek == i)
- {
- if (lista.Where(x => x.x == item.x - i && x.y + i == item.y).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));
- goto folytat;
- }
- }
- else
- continue;
- }
- }
- folytat: continue;
- }
- }
- }
- foreach (var item in lista)
- {
- Console.WriteLine((item.x+1) + " " + (item.y+1) + " - " + item.ertek);
- }
- /*if (lista.Contains(lista.Where(x => x.x == celx && x.y == cely).First()))
- return "yes";
- else
- return "no";*/
- return "yes";
- }
- /************************************* 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 kiirVanMegoldas(Elem[][] tomb)
- {
- for (int i = 0; i < 7; i++)
- {
- Console.Write("\t");
- for (int j = 0; j < 10; j++)
- {
- Console.Write(tomb[i][j].eredmeny + "\t");
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- static void kiirOsszesMegoldas(Elem[][] tomb)
- {
- for (int j = 0; j < 10; j++)
- {
- for (int i = 0; i < 7; i++)
- if (tomb[i][j].eredmeny != 0)
- 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);
- 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;
- String megoldas = keres(tomb, a, b, lista);
- Console.WriteLine(megoldas.Equals("") ? "Nincs megoldas erre a pontra" : megoldas);
- }
- /************************************* 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);
- //osszesMegoldasa(tomb, array);
- kiirArray(array);
- //kiirVanMegoldas(tomb);
- kiirAdottKoordinata(tomb, lista);
- //kiirOsszesMegoldas(tomb);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement