CSES - Putka Open 2015 – 2/6 - Results
Submission details
Task:Sudoku
Sender:
Submission time:2015-08-16 22:34:43 +0300
Language:Java
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.20 sdetails
#2ACCEPTED0.21 sdetails
#3ACCEPTED0.21 sdetails
#4ACCEPTED0.21 sdetails
#5ACCEPTED0.21 sdetails

Code

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static final int KOKO = 81;
    private static final int RIVI = 9;
    private static final int ALITAULU = 3;
    private static final int[] apu = new int[10];

    public static void main(String[] args) {
        Scanner lukija = new Scanner(System.in);

        String ekaRivi = lukija.nextLine();

        int[] taulu = luoTaulu(ekaRivi);

        boolean onnistui = ratko(taulu, 0);

        if (onnistui) {
            System.out.println("");
            for (int i = 1; i <= KOKO; i++) {
                System.out.print(taulu[i - 1]);
                if (i % RIVI == 0) {
                    System.out.println("");
                }
            }
            System.out.println("");
        } else {
            System.out.println("Ei ratkaisua.");
        }
    }

    private static int[] luoTaulu(String ekaRivi) {
        int[] taulu = new int[KOKO];
        int taulunLaskuri = 0;

        for (int i = 0; i < ekaRivi.length(); i++) {
            int luku = Character.getNumericValue(ekaRivi.charAt(i));
            taulu[taulunLaskuri] = luku;
            taulunLaskuri++;
        }

        return taulu;
    }

    private static boolean ratko(int[] taulu, int solu) {
        while (solu < KOKO && taulu[solu] > 0) {
            solu++;
        }

        if (solu == KOKO) {
            return true;
        }

        for (int i = 1; i <= RIVI; i++) {
            taulu[solu] = i;
            if (tarkistaSarake(taulu, solu % RIVI)) {
                if (tarkistaRivi(taulu, solu % RIVI)) {
                    if (tarkistaSolu(taulu, solu % ALITAULU * ALITAULU, solu / RIVI % ALITAULU * ALITAULU)) {
                        if (kelvollinen(taulu) && ratko(taulu, solu + 1)) {
                            return true;
                        }
                    }
                }
            }
        }

        taulu[solu] = 0;
        return false;
    }

    private static boolean tarkistaSarake(int[] taulu, int sarake) {
        Arrays.fill(apu, 0);

        for (int i = 0; i < RIVI; i++) {
            int solu = taulu[i * RIVI + sarake];

            if (solu > 0 && ++apu[solu] > 1) {
                return false;
            }
        }

        return true;
    }

    private static boolean tarkistaRivi(int[] taulu, int rivi) {
        Arrays.fill(apu, 0);

        for (int i = 0; i < RIVI; i++) {
            int solu = taulu[rivi * RIVI + i];

            if (solu > 0 && ++apu[solu] > 1) {
                return false;
            }
        }

        return true;
    }

    private static boolean tarkistaSolu(int[] taulu, int x, int y) {
        Arrays.fill(apu, 0);

        for (int i = 0; i < RIVI; i++) {
            int solu = taulu[(y + i / ALITAULU) * RIVI + (x + i % ALITAULU)];

            if (solu > 0 && ++apu[solu] > 1) {
                return false;
            }
        }

        return true;
    }

    private static boolean kelvollinen(int[] taulu) {
        for (int i = 0; i < 9; i++) {
            if (!tarkistaRivi(taulu, i)) {
                return false;
            }
            if (!tarkistaSarake(taulu, i)) {
                return false;
            }
            if (!tarkistaSolu(taulu, i % ALITAULU * ALITAULU, i / ALITAULU * ALITAULU)) {
                return false;
            }
        }
        return true;
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
592836471

correct output
592836471
836471592
471592836
928364715
364715928
...

user output

592836471
134257689
678149235
213465798
...

Test 2

Verdict: ACCEPTED

input
672935418

correct output
672935418
935418672
418672935
729354186
354186729
...

user output

672935418
134268579
589147236
213456897
...

Test 3

Verdict: ACCEPTED

input
329174658

correct output
329174658
174658329
658329174
291746583
746583291
...

user output

329174658
145268379
678359124
213485796
...

Test 4

Verdict: ACCEPTED

input
376958421

correct output
376958421
958421376
421376958
769584213
584213769
...

user output

376958421
124367589
589124367
213475698
...

Test 5

Verdict: ACCEPTED

input
875694321

correct output
875694321
694321875
321875694
756943218
943218756
...

user output

875694321
123578469
469123578
214356897
...