[算法]游戏'数读’的基本解法 JAVA版。
2014-05-12 17:07
344 查看
昨天被我妹子虐了,做手机数读游戏。
想想毕业到现在这么多年没有写过算法类似的东西了,就写个事实。
首先先看最终版本 : http://www.dream-folio.com:8081/index.html
方框里填数字,计算结果,请不要乱虐,毕竟个把小时的作品,肯定bug很多。
本文记录为主,注释什么的没有。。。代码什么的不能看。
想想毕业到现在这么多年没有写过算法类似的东西了,就写个事实。
首先先看最终版本 : http://www.dream-folio.com:8081/index.html
方框里填数字,计算结果,请不要乱虐,毕竟个把小时的作品,肯定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(" "); } } }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【118-Pascal's Triangle(帕斯卡三角形)】
- HDU 4004 The Frog's Games(基本算法-贪心,搜索-二分)
- 算法面试-基本题-排序 c++&java&php
- 【LeetCode-面试算法经典-Java实现】【118-Pascal's Triangle(帕斯卡三角形)】
- java连接linux中mysql出现:Access denied for user 'root'@'192.168.91.1' (using password: YES)
- <四>java数据结构与算法 插入排序
- java.sql.SQLException: Column count doesn't match value count at row 1
- Java面向对象的基本知识<二>
- JDK1.5使用总结 --《Java 1.5 Tiger: A Developer's Notebook》
- O'Reilly精品图书推荐:数据算法:Hadoop/Spark大数据处理技巧
- leetcode:Pascal's Triangle II 【Java】
- C语言之基本算法39—字符串经典操作
- Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Tim
- 学习Java路上遇到的问题——can't resolved to a type
- Manacher's Algorithm 马拉车算法
- 【java】:selection doesn't contain a maintype
- 深入浅出java虚拟机系列:(二)GC&垃圾收集算法
- NOIP's 算法
- net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'username' has no getter method
- WSDLToJava Error: Thrown by JAXB : undefined element declaration 's:schema'