在此订单中打印2-D数组

时间:2017-09-17 01:33:01

标签: java

首先感谢你的所作所为...... 第二,我有这个包含大量“Char”字母的file.txt。我需要在50X50二维阵列中找到一个单词。

像在矩阵中找到单词拼图一样。

到目前为止,我有这个代码读取文件,并将内容保存为二维数组类型Char,我做了搜索。

现在唯一的问题是我需要打印我在相同的订单中找到的单词,就像在Matrix中一样。

我会附上一张照片,以明确我的观点

Puzzle          word file       Output
n o h t y p s      ruby
m i a r y c c      cave          c
l l e k s a h                      a
r u b y v m e                r u b y v
e h h e l l m                          e
p c j n i c e
r e e k b i p

这是代码。 :

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;


public class ProbSolv1 {



    //////////// .. methods to find the word .. ////////////////

     //////////// method..
     public Set<String> findWords(char[][] puzzle, Set<String> words) {
            Set<String> foundWords = new HashSet<String>();
            int minimumWordLength = findMinimumWordLength(words);
            Set<String> possibleWords = findPossibleWords(puzzle, minimumWordLength);
            for(String word : words) {
                for(String possibleWord : possibleWords) {
                    if(possibleWord.contains(word) || possibleWord.contains(new StringBuffer(word).reverse())) {
                        foundWords.add(word);
                        break;
                    }
                }
            }       
            return foundWords;
        }
           //////////// method..
        private int findMinimumWordLength(Set<String> words) {
            int minimumLength = Integer.MAX_VALUE;
            for(String word : words) {
                if(word.length() < minimumLength)
                    minimumLength = word.length();
            }
            return minimumLength;
        }

        //////////// method..
        private Set<String> findPossibleWords(char[][] puzzle, int minimumWordLength) {
            Set<String> possibleWords = new LinkedHashSet<String>();
            int dimension = puzzle.length; //Assuming puzzle is square
            if(dimension >= minimumWordLength) {
                /* Every row in the puzzle is added as a possible word holder */
                for(int i = 0; i < dimension; i++) {
                    if(puzzle[i].length >= minimumWordLength) {
                        possibleWords.add(new String(puzzle[i]));
                    }
                }
                /* Every column in the puzzle is added as a possible word holder */
                for(int i = 0; i < dimension; i++) {
                    StringBuffer temp = new StringBuffer();
                    for(int j = 0; j < dimension; j++) {
                        temp = temp.append(puzzle[j][i]);
                    }
                    possibleWords.add(new String(temp));
                }
                /* Adding principle diagonal word holders */
                StringBuffer temp1 = new StringBuffer();
                StringBuffer temp2 = new StringBuffer();
                for(int i = 0; i < dimension; i++) {
                    temp1 = temp1.append(puzzle[i][i]);
                    temp2 = temp2.append(puzzle[i][dimension - i - 1]);
                }
                possibleWords.add(new String(temp1));
                possibleWords.add(new String(temp2));
                /* Adding non-principle diagonal word holders */
                for(int i = 1; i < dimension - minimumWordLength; i++) {
                    temp1 = new StringBuffer();
                    temp2 = new StringBuffer();
                    StringBuffer temp3 = new StringBuffer();
                    StringBuffer temp4 = new StringBuffer();
                    for(int j = i, k = 0; j < dimension && k < dimension; j++, k++) {
                        temp1 = temp1.append(puzzle[j][k]);
                        temp2 = temp2.append(puzzle[k][j]);
                        temp3 = temp3.append(puzzle[dimension - j - 1][k]);
                        temp4 = temp4.append(puzzle[dimension - k - 1][j]);
                    }
                    possibleWords.add(new String(temp1));
                    possibleWords.add(new String(temp2));
                    possibleWords.add(new String(temp3));
                    possibleWords.add(new String(temp4));
                }
            }
            return possibleWords;
        }
    ////////////////////.. methods end here ..////////////////////

    public static void main(String[] args) throws IOException {

        // allocate the file..
         File inFile = new File("Puzzle.txt");    
         // scan the file..
            Scanner scanner = new Scanner(inFile); 
        // create an array of type char
            char[][] array = new char[50][50];    
        //assign every letter in the char array
            for(int i=0; i < 50; i++) {
                array[i] = scanner.nextLine().toCharArray();
            }

            // create an object from the same class.
            ProbSolv1 program = new ProbSolv1();

            Set<String> words = new HashSet<String>();

            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the word u want to search :  ");
            String line = scan.nextLine().toUpperCase();    
            words.add(line);
            // searching .. in the array for the word...

            Set<String> wordsFound = program.findWords(array, words);
            for(String word : wordsFound) {
                System.out.println(word);
            }    

enter image description here

1 个答案:

答案 0 :(得分:0)

我看到两种可能的选择:

选项1:创建一个不同的char[][]结构,然后添加您找到的单词。

选项2:创建一个坐标(x,y点)系统来存储单词的坐标,然后打印包含单词的坐标。

我发现你的算法难以理解。但是如果你想使用我创建了一个不同的。我建议你在看我的之前尝试调整你的。

public class Puzzle {

    private final char[][] puzzleInput;
    private final String[] puzzleWords;
    private Set<Point> wordPoints = new HashSet<>();

    public Puzzle(char[][] puzzleInput, String[] puzzleWords) {
        this.puzzleInput = puzzleInput;
        this.puzzleWords = puzzleWords;
        loadPoints();
    }

    public static void main(String[] args) {
        char[][] puzzleInput = {
                {'n', 'o', 'h', 't', 'y', 'p', 's'},
                {'m', 'i', 'c', 'r', 'y', 'c', 'c'},
                {'l', 'l', 'e', 'a', 's', 'a', 'h'},
                {'r', 'u', 'b', 'y', 'v', 'm', 'e'},
                {'e', 'h', 'h', 'e', 'l', 'e', 'm'},
                {'p', 'c', 'j', 'n', 'i', 'c', 'e'},
                {'r', 'e', 'e', 'k', 'b', 'i', 'p'}
        };
        String[] puzzleWords = {"ruby", "cave"};
        Puzzle puzzle = new Puzzle(puzzleInput, puzzleWords);
        puzzle.printPuzzleSolution();
    }

    private void loadPoints() {
        for (int y = 0; y < puzzleInput.length; y++) {
            for (int x = 0; x < puzzleInput[y].length; x++) {
                String horizontalLine = String.valueOf(puzzleInput[y]).substring(x);
                String diagonalLine = buildDiagonalLine(y, x);
                wordPoints.addAll(findHorizontalPoints(x, y, horizontalLine));
                wordPoints.addAll(findDiagonalPoints(x, y, diagonalLine));
            }
        }
    }

    private String buildDiagonalLine(int y, int x) {
        if (x >= puzzleInput[y].length - 1 || y >= puzzleInput.length - 1) {
            return String.valueOf(puzzleInput[y][x]);
        } else {
            String remainer = buildDiagonalLine(y+1, x+1);
            return puzzleInput[y][x] + remainer;
        }
    }

    private Set<Point> findHorizontalPoints(int x, int y, String horizontalLine) {
        Set<Point> result = new HashSet<>();
        for (String word : puzzleWords) {
            if (horizontalLine.startsWith(word)) {
                for (int i = 0; i < word.length(); i++) {
                    result.add(new Point(x+i,y));
                }
            }
        }
        return result;
    }

    private Set<Point> findDiagonalPoints(int x, int y, String diagonalLine) {
        Set<Point> result = new HashSet<>();
        for (String word : puzzleWords) {
            if (diagonalLine.startsWith(word)) {
                for (int i = 0; i < word.length(); i++) {
                    result.add(new Point(x+i, y+i));
                }
            }
        }
        return result;
    }

    public void printPuzzleSolution() {
        for (int y = 0; y < puzzleInput.length; y++) {
            for (int x = 0; x < puzzleInput[y].length; x++) {
                if (isPointInSet(x,y)) {
                    System.out.print(puzzleInput[y][x]);
                } else {
                    System.out.print('.'); // Dot is better to see than white space
                }
            }
            System.out.println();
        }
    }

    private boolean isPointInSet(int x, int y) {
        return wordPoints.contains(new Point(x, y));
    }

}