1. 程式人生 > >JAVA——簡易9*9數獨破解器

JAVA——簡易9*9數獨破解器

package Sudoku_JCoder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class Sudoku  extends JFrame{
    
    static JPanel pan;
    static JButton p[][] = new
JButton[9][9]; static JButton a[][] = new JButton[9][9]; static JButton OK = new JButton("OK"); static JButton Clear = new JButton("Reset"); static int mmp[][] = new int[20][20]; static int inn[][] = new int[20][20]; static Font buttonf = new Font("宋體", Font.PLAIN, 12); static
Font mf = new Font("宋體", Font.PLAIN, 20); static int isOK = 0; static void Print(){ for(int i = 1;i <= 9;i ++) { for(int j = 1;j <= 9;j ++) { a[i - 1][j - 1].setText(String.valueOf(mmp[j][i])); } } return ; }
static int check(int x,int y,int k){ int a = (x + 2) / 3; int b = (y + 2) / 3; for(int i = 1;i <= 9;i ++){ if(mmp[x][i] == k || mmp[i][y] == k){return 0;} } for(int i = 3 * a - 2;i <= 3 * a;i ++){ for(int j = 3 * b - 2;j <= 3 * b;j ++){ if(mmp[i][j] == k){return 0;} } } return 1; } static void Last(){ int a[] = new int[10]; for(int i = 1;i <= 9;i ++){ a[mmp[9][i]] ++; } for(int i = 1;i <= 9;i ++){ if(a[i] == 0){ mmp[9][9] = i; return ; } } return ; } int allcheck(){ int vis[] = new int[20]; for(int i = 1;i <= 9;i ++){ for(int j = 0;j <= 15;j ++) { vis[j] = 0; } for(int j = 1;j <= 9;j ++){ vis[mmp[i][j]] ++; } for(int j = 1;j <= 9;j ++){ if(vis[j] > 1){return 0;} } } for(int i = 1;i <= 9;i ++){ for(int j = 0;j <= 15;j ++) { vis[j] = 0; } for(int j = 1;j <= 9;j ++){ vis[mmp[j][i]] ++; } for(int j = 1;j <= 9;j ++){ if(vis[j] > 1){return 0;} } } for(int i = 1;i <= 3;i ++){ for(int j = 1;j <= 3;j ++){ for(int jj = 0;jj <= 15;jj ++) { vis[jj] = 0; } for(int a = 3 * i - 2;a <= 3 * i;a ++){ for(int b = 3 * j - 2;b <= 3 * j;b ++){ vis[mmp[a][b]] ++; } } for(int p = 1;p <= 9;p ++){ if(vis[p] > 1){return 0;} } } } return 1; } static void dfs(int x,int y){ if(isOK == 1) {return ;} if(x == 9 && y == 9){ Last(); Print(); isOK = 1; return ; } if(y == 10){x ++;y = 1;} if(mmp[x][y] == 0){ for(int i = 1;i <= 9;i ++){ if(check(x,y,i) == 0){continue;} mmp[x][y] = i; dfs(x,y + 1); mmp[x][y] = 0; } } else{ dfs(x,y + 1); } return ; } public Sudoku(){ for(int i = 1;i <= 9;i ++){ for(int j = 0;j < 9;j ++){mmp[i][j] = 0;inn[i][j] = 0;} } pan = new JPanel(); getContentPane().add(pan); pan.setLayout(null); for(int i = 0;i < 9;i ++) { for(int j = 0;j < 9;j ++) { final int ii = i; final int jj = j; p[i][j] = new JButton("0"); p[i][j].setFont(buttonf); pan.add(p[i][j]); p[i][j].setLocation(40 * i + 40,40 * j + 40); p[i][j].setSize(40,40); p[i][j].setVisible(true); p[i][j].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ String b = e.getActionCommand(); int bb = (Integer.valueOf(b) + 1) % 10; p[ii][jj].setText(String.valueOf(bb)); inn[jj + 1][ii + 1] = bb; } }); a[i][j] = new JButton("0"); a[i][j].setFont(buttonf); pan.add(a[i][j]); a[i][j].setLocation(40 * i + 650,40 * j + 40); a[i][j].setSize(40,40); a[i][j].setVisible(true); } } pan.add(OK); OK.setLocation(450,100); OK.setSize(150,60); OK.setVisible(true); OK.setFont(mf); OK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ for(int i = 1;i <= 9;i ++) { for(int j = 1;j <= 9;j ++) { mmp[i][j] = inn[i][j]; } } isOK = 0; if(allcheck() == 1) { dfs(1,1); } else { JOptionPane.showMessageDialog(null, "It's a wrong maze!", "ERROR", JOptionPane.ERROR_MESSAGE); for(int i = 1;i <= 9;i ++) { for(int j = 1;j <= 9;j ++) { a[i - 1][j - 1].setText("0"); p[i - 1][j - 1].setText("0"); mmp[i][j] = 0; inn[i][j] = 0; } } } } }); pan.add(Clear); Clear.setLocation(450,300); Clear.setSize(150,60); Clear.setVisible(true); Clear.setFont(mf); Clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ for(int i = 1;i <= 9;i ++) { for(int j = 1;j <= 9;j ++) { a[i - 1][j - 1].setText("0"); p[i - 1][j - 1].setText("0"); mmp[i][j] = 0; inn[i][j] = 0; } } } }); setSize(1100,500); setTitle("Sudoku Crasher By-J_Coder"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Sudoku(); } }