Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从交点开始在矩阵中插入一个单词_Java_Algorithm_Matrix - Fatal编程技术网

Java 从交点开始在矩阵中插入一个单词

Java 从交点开始在矩阵中插入一个单词,java,algorithm,matrix,Java,Algorithm,Matrix,我对面向对象编程(OOP)和Java还比较陌生(这学期我在我的大学里开始了一门关于这整件事的课程),我有一个关于从交集开始在矩阵中插入一个单词的问题 比如说,我在一个字符串数组中有两个字符串,需要插入到一个10x10字符矩阵(实际上是一个纵横字谜)中,假设这个字符串数组中的单词是“traceroute”和“employee”。如果满足以下条件,这些单词实际上可以填写: 1) 它们不能大于矩阵大小(即单词[n].length()>matrix.length) 2) 它们必须完全插入才能被认为是正常

我对面向对象编程(OOP)和Java还比较陌生(这学期我在我的大学里开始了一门关于这整件事的课程),我有一个关于从交集开始在矩阵中插入一个单词的问题

比如说,我在一个字符串数组中有两个字符串,需要插入到一个10x10字符矩阵(实际上是一个纵横字谜)中,假设这个字符串数组中的单词是“traceroute”和“employee”。如果满足以下条件,这些单词实际上可以填写:

1) 它们不能大于矩阵大小(即单词[n].length()>matrix.length)
2) 它们必须完全插入才能被认为是正常的(即,如果它们发现像“#”字符这样的黑色单元格,则该方法应更改要查找的列和行)。

我想要的是这个pastebin链接里面的东西

我正试图开发一种算法来实现这一点,但我陷入了困境:我曾试图在网上寻找解决方案,但它们没有得到解释,我只是不想复制和粘贴。我想知道为什么会发生这种情况,以及如何避免它。我知道,我正在专注于做一些“不是OO”的事情

这就是我到目前为止所做的

    public void setWord(String[] words) {
    /*
     * 
     * From input, I receive a String arrays which holds two words --->
     * 
     * 
     * String[] daWords = { "traceroute", "pippo"};
     * 
     * At this point, I thought to create a new boolean var(which I actually turned into an array)
     * which, when it'll be setted to true, will tell to the crossword to change word in order to
     * avoid an ongoing printing of the same string.

     */

    boolean[] hasItBeenUsed = { false, false, false, false};
    //the indexes of the strings array and of the boolean array
    /*
     * 
     * Essentialy, the boolean array must have a correspondence with the string one.
     * In a better way: hasItBeenUsed[0] will become true <=> words[0] has been fully
     * inserted inside the crossword.
     */
    int counterForBoolArr = 0;
    int counterForStringArr = 0;


for(int i = 0; i < 10; i++) {


        for(int j = 0; j < 10; j++) {
            /*
             * Variable which, according to my twisted mind, should represent the end of the insertion
             * of the current word
             */
            boolean breaking = words[counterForStringArr].length() < j;
            /*
             * If the element situated in pos matrix[i][j] doesn't contain a blackcell(#) and the
             * boolean variable is setted to false...
             */

            if (matrix[i][j] != '#' && !breaking) {
                //Add in matrix[i][j] the char contained in words[counterForStringArr].charAt(j)
                matrix[i][j] = words[counterForStringArr].charAt(j);
            } else {
                //Continue the iteration and do not consider this case
                continue;
            }
            //If the boolean variable is actually setted to true...
                if (breaking) {
                    /*
                     * I check if the char contained in matrix[i][j] is the same of the given word from the Strings array.
                     * If it's a yes, the element contained in hasItBeenUsed[counterForBoolArr] will be setted either to true or false.
                     * 
                     */


                    hasItBeenUsed[counterForBoolArr] = matrix[i][j] == words[counterForStringArr].charAt(j) ? true : false;

                    /*
                     * Increasing the indexes in order to pass to the next correspondence between String-boolean
                     */


                    counterForBoolArr++;
                    counterForStringArr++;
                    }

        }

    }

}
public void设置字(字符串[]字){
/*
* 
*从输入中,我收到一个字符串数组,其中包含两个单词-->
* 
* 
*字符串[]daWords={“traceroute”,“pippo”};
* 
*在这一点上,我想创建一个新的布尔变量(实际上我把它变成了一个数组)
*当设置为true时,它将告诉纵横字谜更改单词以
*避免持续打印同一字符串。
*/
布尔[]hasItBeenUsed={false,false,false,false};
//字符串数组和布尔数组的索引
/*
* 
*基本上,布尔数组必须与字符串1对应。
*以更好的方式:hasItBeenUsed[0]将成为真实的文字[0]已完全
*插入填字游戏中。
*/
int-counterForBoolArr=0;
int-counterstorstringar=0;
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
/*
*根据我扭曲的想法,变量应该代表插入的结束
*当前单词的名称
*/
布尔中断=字[counterforstringar].length()
我从这个方法中得到的是一个填字矩阵,它只由一个单词“traceroute”填充,而这实际上并不是我想要的输出,我不知道如何停止或跳转到下一个单词。我认为,声明布尔破缺变量将有助于我实现这一目标,但看起来我所做的唯一一件事就是让这段代码更不可读。我也想过使用“break;”公式,但它只会打印一个单词,就是这样

有人能帮我建议一下我该如何正确地写这篇文章吗


任何事情都将不胜感激

你想填填填填字游戏的答案吗?如果是这样,对于初学者来说,这是一个极其复杂的算法。你必须在所有位置尝试每个单词,当一个单词不合适时停止,用另一个单词重新开始,直到你尝试了所有单词的所有组合。是的@GilbertLeBlanc,我正在尝试这样做,因为它是作为家庭作业提供的。我如何实现“当一个词不合适时停止”部分?我考虑过跟踪纵横字谜中插入了多少个字符,然后比较字符串的长度和跟踪变量所包含的数字。这可能是一种有效的方法吗?“我如何实现“当一个单词不适合时停止”部分?”从矩阵中获取水平或垂直区域的大小,并查看该单词是否适合。将你的代码分解成微小的、易于测试的方法,否则你将永远无法完成任务。非常感谢你的建议@GilbertLeBlanc!你想填填填填字游戏的答案吗?如果是这样,对于初学者来说,这是一个极其复杂的算法。你必须在所有位置尝试每个单词,当一个单词不合适时停止,用另一个单词重新开始,直到你尝试了所有单词的所有组合。是的@GilbertLeBlanc,我正在尝试这样做,因为它是作为家庭作业提供的。我如何实现“当一个词不合适时停止”部分?我曾想过跟踪填字游戏中插入了多少个字符,然后是com