Informatika gyűjtemény

Egy szinttel feljebb tb_sudoku.cs

2004050607080910

NézetNyomtat

tb_sudoku.cs (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 2 KB
using System;

namespace Sudoku
{
    class Mezo
    {
        public int ertek = 0;
        public bool[] lehet = new bool[9];

        public Mezo()
        {
            for (int i = 0; i < 9; i++)
                lehet[i] = true;
        }
    }

    class Program
    {
        const string InputFile  = "sudoku1.be";
        const string OutputFile = "sudoku1.ki";

        Mezo[,] mezok = new Mezo[9, 9];

        private Program()
        {
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 9; j++)
                    mezok[i, j] = new Mezo();
        }

        private void Betolt()
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(InputFile);
            string[] sor;

            for (int i = 0; i < 9; i++) {
                sor = sr.ReadLine().Split();
                for (int j = 0; j < 9; j++)
                    Betesz(i, j, int.Parse(sor[j]));
            }
            sr.Close();
        }

        private void Betesz(int sor, int oszlop, int ertek)
        {
            if (sor < 0 || sor >= 9 || oszlop < 0 || oszlop >= 9 || ertek < 1 || ertek > 9)
                return;

            mezok[sor, oszlop].ertek = ertek;

            for (int i = 0; i < 9; i++) {
                mezok[sor, i].lehet[ertek - 1] = false;
                mezok[i, oszlop].lehet[ertek - 1] = false;
                mezok[sor, oszlop].lehet[i] = false;
            }

            for (int i = (sor / 3) * 3; i < (sor / 3 + 1) * 3; i++)
                for (int j = (oszlop / 3) * 3; j < (oszlop / 3 + 1) * 3; j++)
                    mezok[i, j].lehet[ertek - 1] = false;
        }

        private bool UjatKeres()
        {
            const int Nincs = -1, TobbVan = -2;
            int index1, index2, index3;
            bool talalat = false;

            for (int ertek = 1; ertek <= 9; ertek++) {
                for (int i = 0; i < 9; i++) {
                    index1 = index2 = index3 = Nincs;

                    for (int j = 0; j < 9; j++) {
                        // Soronként keres
                        if (mezok[i, j].lehet[ertek - 1]) {
                            if (index1 == Nincs)
                                index1 = j;
                            else
                                index1 = TobbVan;
                        }

                        // Oszloponként keres
                        if (mezok[j, i].lehet[ertek - 1]) {
                            if (index2 == Nincs)
                                index2 = j;
                            else
                                index2 = TobbVan;
                        }

                        // 3×3-as mezőnként keres
                        if (mezok[(/ 3) * 3 + (/ 3), (% 3) * 3 + (% 3)].lehet[ertek - 1]) {
                            if (index3 == Nincs)
                                index3 = j;
                            else
                                index3 = TobbVan;
                        }
                    }

                    if (index1 >= 0) {
                        Betesz(i, index1, ertek);
                        talalat = true;
                    }
                    if (index2 >= 0) {
                        Betesz(index2, i, ertek);
                        talalat = true;
                    }
                    if (index3 >= 0) {
                        Betesz((/ 3) * 3 + (index3 / 3), (% 3) * 3 + (index3 % 3), ertek);
                        talalat = true;
                    }
                }
            }
            
            return talalat;
        }

        private void Kiir()
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(OutputFile);

            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++)
                    sw.Write(mezok[i, j].ertek + " ");
                sw.WriteLine();
            }
            sw.Close();
        }

        private void Megold()
        {
            Betolt();
            while (UjatKeres())
                ;
            Kiir();
        }

        public static void Main(string[] args)
        {
            Program program = new Program();
            program.Megold();
        }
    }
}
(Vissza)