时差爆发,睡不着,闲的蛋疼。。。思路写在注释里,直接撸代码:
import java.util.*; public class WordPuzzle { private char[][] puzzle; private List<String> wordList; public WordPuzzle(char[][] puzzle, List<String> wordList) { this.puzzle = puzzle; this.wordList = wordList; } /** * Solve the puzzle.<br/> * result: {beginIndex(row, column);endIndex(row, column);matchWord}<br/> * temp tuple: {beginIndex(row,column);endIndex(row,column);direction;composedChars2String}<br/> * Procedure:<br/> * for each element in the puzzle {<br/> * construct a tuple;<br/> * check word list and see whether there is a match;<br/> * } */ public List<String> solvePuzzle() { List<String> resultList = new LinkedList<String>(); List<String> tupleList = null; String tempResult = null; String[] strings = null; for(int i = 0; i < puzzle.length; i++) { for(int j = 0; j < puzzle[i].length; j++) { tupleList = findAllString(i, j); System.out.println("Tuple list constructed."); for(String temp : tupleList) { System.out.println(temp); if(temp != null) { strings = temp.split(";"); System.out.println("judging " + strings[0]); if(wordList.contains(strings[0])) { tempResult = "(" + i + "," + j + ");" + strings[1] + ";" + strings[0]; resultList.add(tempResult); } } } } } Iterator<String> iterator = resultList.iterator(); String temp = null; while(iterator.hasNext()) { temp = iterator.next(); if(temp == null || "null".equals(temp)) { iterator.remove(); } } return resultList; } /** * Find all possible composed strings and return as a list along with the end index.<br/> * eg. {string;(row,column)} */ private List<String> findAllString(int beginRow, int beginColumn) { List<String> resultList = new LinkedList<String>(); List<String> tempList = null; if(beginColumn < puzzle[beginRow].length - 1) { tempList = findAllRow(beginRow, beginColumn); appendList(resultList, tempList); tempList.clear(); } if(beginColumn < puzzle.length - 1) { tempList = findAllColumn(beginRow, beginColumn); appendList(resultList, tempList); tempList.clear(); } if((beginColumn < puzzle.length - 1) && (beginColumn < puzzle[beginRow].length - 1)) { tempList = findAllDiagonal(beginRow, beginColumn); appendList(resultList, tempList); tempList.clear(); } return resultList; } /** * Appends each element form the src list to the destination list. */ public <T> void appendList(List<T> dest, List<T> src) { for (T temp : src) { dest.add(temp); } } /** * Find all possible composed words in the same row starting at a given index. */ private List<String> findAllRow(int i, int j) { List<String> tempWords = new LinkedList<String>(); StringBuilder sb = new StringBuilder(); // Hold the words and their indexes. StringBuilder sbw = new StringBuilder(); // Hold the composed words. if(j < puzzle[i].length - 1) { sbw.append(puzzle[i][j]); for(int a = j + 1; a < puzzle[i].length; a++) { sbw.append(puzzle[i][a]); sb.append(sbw.toString()).append(";(") .append(i).append(',').append(a).append(')'); System.out.println(sbw.toString()); System.out.println(sb.toString()); tempWords.add(sb.toString()); sb.delete(0, sb.length()); // Empty the sb. } } return tempWords; } /** * Find all possible composed words in the same column starting at a given index. */ private List<String> findAllColumn(int i, int j) { List<String> tempWords = new LinkedList<String>(); StringBuilder sb = new StringBuilder(); // Hold the words and their indexes. StringBuilder sbw = new StringBuilder(); // Hold the composed words. if(i < puzzle.length - 1) { sbw.append(puzzle[i][j]); for(int a = i + 1; a < puzzle.length; a++) { sbw.append(puzzle[a][j]); sb.append(sbw.toString()).append(";(") .append(a).append(',').append(j).append(')'); System.out.println(sbw.toString()); System.out.println(sb.toString()); tempWords.add(sb.toString()); sb.delete(0, sb.length()); // Empty the sb. } } return tempWords; } /** * Find all possible composed words in the diagonal starting at a given index. */ private List<String> findAllDiagonal(int i, int j) { List<String> tempWords = new LinkedList<String>(); StringBuilder sb = new StringBuilder(); StringBuilder sbw = new StringBuilder(); if(i < puzzle.length - 1 && j < puzzle[i].length - 1) { sbw.append(puzzle[i][j]); for(int a = i + 1, b = j + 1; a < puzzle.length && b < puzzle[a].length; a++, b++) { sbw.append(puzzle[a][b]); sb.append(sbw.toString()).append(";(") .append(a).append(',').append(b).append(')'); System.out.println(sbw.toString()); System.out.println(sb.toString()); tempWords.add(sb.toString()); sb.delete(0, sb.length()); // Empty the sb. } } return tempWords; } public static void main(String[] args) { List<String> dictionary = new ArrayList<String>(); Collections.addAll(dictionary, new String[]{"dad", "cat", "sad", "sat", "bad"}); char[][] myPuzzle = new char[4][4]; myPuzzle[0] = new char[] {'i', 'd', 'a', 'd'}; myPuzzle[1] = new char[] {'w', 's', 'a', 'd'}; myPuzzle[2] = new char[] {'v', 'b', 'a', 'd'}; myPuzzle[3] = new char[] {'z', 'c', 'a', 't'}; WordPuzzle wp = new WordPuzzle(myPuzzle, dictionary); List<String> results = wp.solvePuzzle(); System.out.println("the result is: "); for(String temp : results) { System.out.println(temp); } } }
运行结果:
the result is: (0,1);(0,3);dad (0,1);(2,3);dad (1,1);(1,3);sad (1,1);(3,3);sat (2,1);(2,3);bad (3,1);(3,3);cat
相关推荐
该项目的目的是用 python 创建一个游戏,向用户呈现一个单词的字谜,并且必须在有限的尝试次数内猜出正确的单词。 项目特点: 1.用户被给予固定次数的尝试来猜测正确的单词。尝试次数取决于单词的长度。 2.每次...
Java作业4字谜游戏
对于数独字谜的解法,程序简介,适合初学者使用。节省时间试试看裁决之镰测试正常上课尝试几次生产基地参加第几
c语言数独字谜游戏课程设计.doc
40个有趣的英语字谜游戏.pdf
汉字字谜,已经整理成数据库脚本可直接导入,总计3860个。
字谜大全及答案.
玩15字谜的很不错哦
3d阿福图库字谜总汇.doc
一个用netbeans编写的小游戏,界面做得挺漂亮的
matlab开发-matlab竞赛纵横字谜。为Matlab®编程比赛开发和评分所需的所有文件。
已经做好的程序,也行对某些人有用
七巧板游戏 (2015年7月-)创建一个字谜游戏,该游戏将用于收集有关协作创造力的数据
字谜节点用 node.js 制作的字谜游戏
NULL 博文链接:https://gaoquanyang.iteye.com/blog/1520076
包含3860个字谜和谜底,txt格式,导入到数据库中和小伙伴一起做游戏吧。
小学生字谜大全.doc
小学生字谜精选.doc