Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Sudoku
- {
- public static void Main(string[] args)
- {
- //byte[,] plocha = new byte[9, 9];
- //byte aktivniKelii = 0;
- //string vlez;
- //bool aktivnost = true, validenVlez = false;
- byte[,] plocha = new byte[,] {
- {0, 2, 5, 0, 0, 1, 0, 0, 0},
- {1, 0, 4, 2, 5, 0, 0, 0, 0},
- {0, 5, 0, 0, 0, 0, 3, 2, 0},
- {6, 0, 0, 0, 2, 0, 0, 0, 9},
- {0, 8, 7, 0, 0, 0, 0, 6, 0},
- {0, 9, 1, 5, 0, 6, 6, 0, 0},
- {0, 9, 1, 5, 0, 0, 6, 0, 0},
- {0, 0, 0, 0, 7, 8, 1, 0, 3},
- {0, 0, 0, 6, 0, 0, 5, 9, 0},
- };
- solve(plocha);
- /*
- while (aktivnost)
- {
- Console.WriteLine("Vnesi komanda (-iskluchi, -prikazhi, -vmetni, -reshi):\r\n");
- vlez = Console.ReadLine();
- switch (vlez)
- {
- case "-reshi":
- {
- for (byte x = 0; x < 9; x++)
- {
- for (byte y = 0; y < 9; y++)
- {
- if (plocha[x, y] != 0) aktivniKelii++;
- if (aktivniKelii > 17)
- {
- validenVlez = true;
- break;
- }
- }
- }
- if (!validenVlez)
- {
- Console.WriteLine("\r\nVmetni barem 17 tragi. Obidi se povtorno!\r\n");
- break;
- }
- solve(plocha);
- break;
- }
- case "-prikazhi":
- {
- Console.WriteLine();
- for (byte x = 0; x < 9; x++)
- {
- for (byte y = 0; y < 9; y++)
- {
- if (y == 8)
- {
- Console.WriteLine(plocha[x, y]);
- continue;
- }
- Console.Write(plocha[x, y] + " ");
- }
- }
- Console.WriteLine();
- break;
- }
- case "-vmetni":
- {
- Console.WriteLine("\r\nVnesi pozicija i traga (format: redica kolona vrednost):\r\n");
- string baferVlez = Console.ReadLine();
- plocha[Byte.Parse(baferVlez[0].ToString()), Byte.Parse(baferVlez[2].ToString())] = Byte.Parse(baferVlez[4].ToString());
- Console.WriteLine("\r\nVmetnato.\r\n");
- break;
- }
- case "-iskluchi":
- {
- aktivnost = false;
- Console.WriteLine("\r\nAdios.");
- break;
- }
- default:
- {
- Console.Write("\r\nNevalidna komanda! ");
- break;
- }
- }
- }
- */
- }
- static void solve(byte[,] plocha)
- {
- List<List<int>> validSet = new List<List<int>>();
- List<int> validRow = new List<int>();
- List<int> validCol = new List<int>();
- List<int> validBox = new List<int>();
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- int[] currRow = new int[9];
- int[] currCol = new int[9];
- int[] currBox = new int[9];
- int k = 0;
- // Tekovna Redica
- for (int cntJ = 0; cntJ < 9; cntJ++)
- {
- if (plocha[i, cntJ] != 0) currRow[k] = plocha[i, cntJ];
- k++;
- }
- // Tekovna Kolona
- k = 0;
- for (int cntI = 0; cntI < 9; cntI++)
- {
- if (plocha[cntI, j] != 0) currCol[k] = plocha[cntI, j];
- k++;
- }
- // Tekoven Region
- k = 0;
- int regSize = 3;
- int subBoxI = i / regSize;
- int subBoxJ = j / regSize;
- int offsetI = regSize * subBoxI;
- int offsetJ = regSize * subBoxJ;
- for (int r = 0; r < 3; r++)
- {
- for(int c = 0; c < 3; c++)
- {
- if (plocha[offsetI + r, offsetJ + c] != 0) currBox[k] = plocha[offsetI + r, offsetJ + c];
- k++;
- }
- }
- if (plocha[i, j] == 0)
- {
- for (int r = 1; r < 10; r++)
- {
- if (Array.IndexOf(currRow, r) < 0) validRow.Add(r);
- if (Array.IndexOf(currCol, r) < 0) validCol.Add(r);
- if (Array.IndexOf(currBox, r) < 0) validBox.Add(r);
- }
- List<int> temp = new List<int>();
- for (int r = 1; r < 10; r++)
- {
- if (validRow.Contains(r) && validCol.Contains(r) && validBox.Contains(r))
- {
- temp.Add(r);
- }
- }
- validSet.Add(temp);
- }
- }
- }
- foreach (var item in validSet)
- {
- foreach (var obj in item)
- {
- Console.Write(obj + ", ");
- }
- }
- }
- void pripremiPlocha(byte[,] plocha)
- {
- for (byte x = 0; x < 9; x++)
- {
- for (byte y = 0; y < 9; y++)
- {
- plocha[x, y] = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement