qwerty787788

Sudoku Solver

Feb 25th, 2013
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solver {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     int n = 9;
  9.     char[][] a = new char[n][n];
  10.     boolean[] was = new boolean[n + 1];
  11.  
  12.     boolean isOk() {
  13.         for (int i = 0; i < n; i++) {
  14.             Arrays.fill(was, false);
  15.             for (int j = 0; j < n; j++) {
  16.                 int x = a[i][j] == '.' ? -1 : a[i][j] - '0';
  17.                 if (x == -1)
  18.                     continue;
  19.                 if (was[x])
  20.                     return false;
  21.                 was[x] = true;
  22.             }
  23.         }
  24.         for (int i = 0; i < n; i++) {
  25.             Arrays.fill(was, false);
  26.             for (int j = 0; j < n; j++) {
  27.                 int x = a[j][i] == '.' ? -1 : a[j][i] - '0';
  28.                 if (x == -1)
  29.                     continue;
  30.                 if (was[x])
  31.                     return false;
  32.                 was[x] = true;
  33.             }
  34.         }
  35.         for (int stX = 0; stX < n; stX += n / 3)
  36.             for (int stY = 0; stY < n; stY += n / 3) {
  37.                 Arrays.fill(was, false);
  38.                 for (int j = 0; j < n; j++) {
  39.                     int dx = j % (n / 3);
  40.                     int dy = j / (n / 3);
  41.                     int x = a[stX + dx][stY + dy] == '.' ? -1 : a[stX + dx][stY
  42.                             + dy] - '0';
  43.                     if (x == -1)
  44.                         continue;
  45.                     if (was[x])
  46.                         return false;
  47.                     was[x] = true;
  48.                 }
  49.             }
  50.         return true;
  51.     }
  52.  
  53.     boolean go(int fromX, int fromY) {
  54.         while (fromX < n) {
  55.             if (fromY >= n) {
  56.                 fromY = 0;
  57.                 fromX++;
  58.             }
  59.             if (fromX == n)
  60.                 break;
  61.             if (a[fromX][fromY] != '.') {
  62.                 fromY++;
  63.             } else {
  64.                 break;
  65.             }
  66.         }
  67.         if (fromX == n)
  68.             return true;
  69.         for (int x = 1; x <= n; x++) {
  70.             char c = (char) ('0' + x);
  71.             a[fromX][fromY] = c;
  72.             if (isOk()) {
  73.                 if (go(fromX, fromY + 1)) {
  74.                     return true;
  75.                 } else {
  76.                     a[fromX][fromY] = '.';
  77.                 }
  78.             } else {
  79.                 a[fromX][fromY] = '.';
  80.             }
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     void solve() {
  86.         long time = System.currentTimeMillis();
  87.         for (int i = 0; i < n; i++) {
  88.             String s = in.next();
  89.             for (int j = 0; j < n; j++)
  90.                 a[i][j] = s.charAt(j);
  91.         }
  92.         if (isOk())
  93.             go(0, 0);
  94.         for (int i = 0; i < n; i++) {
  95.             for (int j = 0; j < n; j++) {
  96.                 out.print(a[i][j]);
  97.             }
  98.             out.println();
  99.         }
  100.         System.err.println(System.currentTimeMillis() - time);
  101.     }
  102.  
  103.     void run() {
  104.         try {
  105.             in = new FastScanner(new File("sudoku.in"));
  106.             out = new PrintWriter(new File("sudoku.out"));
  107.  
  108.             solve();
  109.  
  110.             out.close();
  111.         } catch (FileNotFoundException e) {
  112.             e.printStackTrace();
  113.         }
  114.     }
  115.  
  116.     void runIO() {
  117.  
  118.         in = new FastScanner(System.in);
  119.         out = new PrintWriter(System.out);
  120.  
  121.         solve();
  122.  
  123.         out.close();
  124.     }
  125.  
  126.     class FastScanner {
  127.         BufferedReader br;
  128.         StringTokenizer st;
  129.  
  130.         public FastScanner(File f) {
  131.             try {
  132.                 br = new BufferedReader(new FileReader(f));
  133.             } catch (FileNotFoundException e) {
  134.                 e.printStackTrace();
  135.             }
  136.         }
  137.  
  138.         public FastScanner(InputStream f) {
  139.             br = new BufferedReader(new InputStreamReader(f));
  140.         }
  141.  
  142.         String next() {
  143.             while (st == null || !st.hasMoreTokens()) {
  144.                 String s = null;
  145.                 try {
  146.                     s = br.readLine();
  147.                 } catch (IOException e) {
  148.                     e.printStackTrace();
  149.                 }
  150.                 if (s == null)
  151.                     return null;
  152.                 st = new StringTokenizer(s);
  153.             }
  154.             return st.nextToken();
  155.         }
  156.  
  157.         boolean hasMoreTokens() {
  158.             while (st == null || !st.hasMoreTokens()) {
  159.                 String s = null;
  160.                 try {
  161.                     s = br.readLine();
  162.                 } catch (IOException e) {
  163.                     e.printStackTrace();
  164.                 }
  165.                 if (s == null)
  166.                     return false;
  167.                 st = new StringTokenizer(s);
  168.             }
  169.             return true;
  170.         }
  171.  
  172.         int nextInt() {
  173.             return Integer.parseInt(next());
  174.         }
  175.  
  176.         long nextLong() {
  177.             return Long.parseLong(next());
  178.         }
  179.     }
  180.  
  181.     public static void main(String[] args) {
  182.         new Solver().run();
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment