[演算法]遊戲'數讀’的基本解法 JAVA版。
阿新 • • 發佈:2019-02-17
昨天被我妹子虐了,做手機數讀遊戲。
想想畢業到現在這麼多年沒有寫過演算法類似的東西了,就寫個事實。
方框裡填數字,計算結果,請不要亂虐,畢竟個把小時的作品,肯定bug很多。
本文記錄為主,註釋什麼的沒有。。。程式碼什麼的不能看。
/* * @(#) A.java 2014-5-12 * * Copyright 2010 NetEase.com, Inc. All rights reserved. */ package cn.zdfzboy.algorithm.test; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; /** * {1,9,6,0,0,0,0,0,0}, {0,0,0,0,0,8,3,0,0}, {0,0,2,1,9,0,0,0,0}, {0,2,0,0,8,0,0,0,7}, {0,0,5,2,0,1,8,0,0}, {7,0,0,0,6,0,0,3,0}, {0,0,0,0,1,6,4,0,0}, {0,0,7,4,0,0,0,0,0}, {0,0,0,0,0,0,9,8,5}, ---------------------- {7,0,8,0,0,0,3,0,0}, {0,0,0,2,0,1,0,0,0}, {5,0,0,0,0,0,0,0,0}, {0,4,0,0,0,0,0,2,6}, {3,0,0,0,8,0,0,0,0}, {0,0,0,1,0,0,0,9,3}, {0,9,0,6,0,0,0,0,4}, {0,0,0,0,7,0,5,0,0}, {0,0,0,0,0,0,0,0,0}, * @author JWW * */ public class Readings { public boolean stop = false; public int[][] last; public static void main(String args[]){ int[][] m = { {3,0,0,9,0,0,0,0,0}, {0,1,0,0,2,0,0,5,0}, {0,0,0,0,0,8,0,0,6}, {8,0,0,7,0,0,3,0,0}, {0,3,0,0,5,0,0,1,0}, {0,0,7,0,0,6,0,0,2}, {6,0,0,0,0,0,8,0,0}, {0,2,0,0,1,0,0,0,0}, {0,0,0,0,0,9,0,0,0}, }; Readings r = new Readings(); r.getTab(m, 6); r.earth(m); r.print(r.last); } public boolean check(int[][] m,int value,int i,int j){ for(int k=0;k<9;k++){ if(m[i][k] == value || m[k][j] == value) return false; } return true; } public void earth(int[][] m){ if(stop){ return; } List<PossibleNumber> pn = getPossibleNumber(m); if(pn.size() == 0 ){ last = m; stop = true; return; } boolean tag = false; for(PossibleNumber p : pn){ if(p.size == 0) { return; } if(p.size == 1){ tag = true; if(check(m,p.possible[0],p.i,p.j)){ m[p.i][p.j] = p.possible[0]; }else{ return; } } } if(tag){ earth(m); return; } for(PossibleNumber p : pn){ if(p.size == 2){ int[][] c1 = clone(m); c1[p.i][p.j] = p.possible[0]; int[][] c2 = clone(m); c2[p.i][p.j] = p.possible[1]; earth(c1); earth(c2); return; } } System.out.println("=-"); } public int[][] clone(int[][] m){ int[][] copy=new int[m.length][]; for(int i=0;i<m.length;i++){ copy[i]=Arrays.copyOf(m[i], m[i].length); } return copy; } public List<PossibleNumber> getPossibleNumber(int[][] m){ Integer[] empty = {}; List<PossibleNumber> l = new ArrayList<PossibleNumber>(); for(int i=0;i<m.length;i++){ for(int j=0;j<m[i].length;j++){ if(m[i][j] == 0){ PossibleNumber p = new PossibleNumber(); p.i = i; p.j = j; HashSet<Integer> h = init(); for(int z : getTab(m,((i/3)*3)+j/3)){ if(z == 0) continue; h.remove(z); } for(int k=0;k<9;k++){ if(m[i][k] != 0) h.remove(m[i][k]); if(m[k][j] != 0) h.remove(m[k][j]); } p.possible = h.toArray(empty); p.size = h.size(); l.add(p); } } } return l; } public int[] getTab(int[][] mia,int k){ int[] ss = new int[9]; int ii =0; for(int i=(k/3)*3;i<(k/3)*3 + 3;i++){ for(int j=(k%3)*3;j<(k%3)*3 + 3;j++){ ss[ii] = mia[i][j]; ii++; } } return ss; } public HashSet<Integer> init(){ HashSet<Integer> h = new HashSet<Integer>(); for(int i = 1;i <= 9;i++){ h.add(i); } return h; } public void print(int[][] m){ for(int i=0;i<m.length;i++){ for(int j=0;j<m[i].length;j++){ System.out.print(m[i][j] + " "); } System.out.println(" "); } } }