Направление печати java - PullRequest
0 голосов
/ 14 июля 2020

У меня есть код, чтобы найти позицию слова из сетки 10x10, и я хочу, чтобы он выводил его, как показано ниже:

added - Pattern found vertically at 10, 8

Но теперь я получаю много операторов печати каждого движения и есть некоторые движения для слов, которые не печатаются. Я попытался перенести метод принтера в patternSearch, но результат тот же. Однако, если я удалю метод принтера и не реализую вывод движения, он будет работать хорошо, как показано ниже:

added - Pattern found at 10, 8

Но мне действительно нужно движение, так как будет легче определить расположение всего слова в сетка

Вот код:

import java.io.*; 
import java.util.*; 
  
class Main { 
  
    
    static int R, C; 
  
    // For searching in all 8 direction 
    static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 }; 
    static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 }; 
    
    static boolean search2D(char[][] grid, int row, 
                            int col, String word) 
    { 
        
        
        if (grid[row][col] != word.charAt(0)) 
            return false; 
  
        int len = word.length(); 
  
        // Search word in all 8 directions 
        // starting from (row, col) 
        for (int dir = 0; dir < 8; dir++) { 
            
            //Make current direction starting point 
            int k, rd = row + x[dir], cd = col + y[dir]; 
  
            // First character is already checked, then check others
            for (k = 1; k < len; k++) { 
                
                if (rd >= R || rd < 0 || cd >= C || cd < 0) 
                    break; 
  
               
                if (grid[rd][cd] != word.charAt(k)) 
                    break; 
  
                // Moving in that direction 
                
                rd += x[dir];
                
                cd += y[dir]; 
                
                
            } 
  
            // If all character matched,value equal to length of word
            if (k == len) 
                return true; 
        } 
        
    
        
        return false; 
    } 
  
    static String printer(char[][] grid, int row, int col, String word) {
        
        String move="";
        char found=word.charAt(1);
        int g=row;
        int h=col;
        
        if(grid[g+1][h]==found){
            move="vertically";
            
        }
        
        else if(grid[g][h+1]==found){
            move="horizontally";
            
        }
        
        else if(grid[g-1][h]==found){
            move="vertically";
            
        }
        
        else if(grid[g][h-1]==found){
            move="horizontally";
            
        }
        
        else if(grid[g-1][h-1]==found){
            move="diag";
            
        }
        
        else if(grid[g+1][h+1]==found){
            move="diag";
            
        }

        else if(grid[g-1][h+1]==found){
            move="diag";
        
        }
        else if(grid[g+1][h-1]==found){
            move="diag";
        
        }
        
        
        return move;
        
    }
    
    static void patternSearch( char[][] grid, String word) { 
        String mov="";
        for (int row = 0; row < R; row++) { 
            for (int col = 0; col < C; col++) { 
                
                if (search2D(grid, row, col, word)) 
                    mov=printer(grid, row,col,word);
                    System.out.println(word +" - Pattern found " + mov +" at " + (row+1) + ", " + (col+1));
                    
                
            } 
            
        } 
    } 
  
    
    public static void main(String args[]) 
    { 
        R = 10; 
        C = 10; 
        char[][] grid = { 
            
            {'t', 'a', 'b', 'o', 'v', 'e', 'o', 'y', 'm', 'z'},
            {'s', 'u', 'q', 'o', 'e', 'a', 'v', 'i', 'u', 's'},
            {'j', 'f', 'o', 'e', 'o', 'a', 'd', 'l', 'f', 'w'},
            {'t', 'h', 'r', 'b', 'q', 'f', 'b', 'u', 'x', 'z'},
            {'d', 'g', 'r', 't', 'a', 't', 'y', 'u', 'l', 'p'},
            {'a', 'w', 'c', 's', 'n', 'e', 't', 'd', 's', 't'},
            {'z', 's', 'w', 'e', 'i', 'r', 'u', 'e', 'q', 'e'},
            {'q', 'v', 'g', 'g', 'a', 'x', 'l', 'd', 'z', 'z'},
            {'v', 'a', 'n', 'i', 'g', 'r', 'p', 'd', 'y', 't'},
            {'c', 'u', 'd', 'j', 'a', 'w', 'd', 'a', 'n', 'k'}
            
        }; 
        
        for (int row = 0; row < R; row++) { 
            for (int col = 0; col < C; col++) { 
                System.out.print(grid[row][col]+"\t");
                
            }
            System.out.println();
        }
        
        System.out.println();
        patternSearch(grid, "about");

        patternSearch(grid, "above");

        patternSearch(grid, "abuse");

        patternSearch(grid, "added");
 
        patternSearch(grid, "adult");
 
        patternSearch(grid, "after");

        patternSearch(grid, "again");
 
        patternSearch(grid, "agent");

        patternSearch(grid, "agree");
   
        
    } 
} 
  

Мой текущий вывод:

t   a   b   o   v   e   o   y   m   z   
s   u   q   o   e   a   v   i   u   s   
j   f   o   e   o   a   d   l   f   w   
t   h   r   b   q   f   b   u   x   z   
d   g   r   t   a   t   y   u   l   p   
a   w   c   s   n   e   t   d   s   t   
z   s   w   e   i   r   u   e   q   e   
q   v   g   g   a   x   l   d   z   z   
v   a   n   i   g   r   p   d   y   t   
c   u   d   j   a   w   d   a   n   k   

about - Pattern found  at 1, 1
about - Pattern found  at 1, 2
about - Pattern found  at 1, 3
about - Pattern found  at 1, 4
about - Pattern found  at 1, 5
about - Pattern found  at 1, 6
about - Pattern found  at 1, 7
about - Pattern found  at 1, 8
about - Pattern found  at 1, 9
about - Pattern found  at 1, 10
about - Pattern found  at 2, 1
about - Pattern found  at 2, 2
about - Pattern found  at 2, 3
about - Pattern found  at 2, 4
about - Pattern found  at 2, 5
about - Pattern found  at 2, 6
about - Pattern found  at 2, 7
about - Pattern found  at 2, 8
about - Pattern found  at 2, 9
about - Pattern found  at 2, 10
about - Pattern found  at 3, 1
about - Pattern found  at 3, 2
about - Pattern found  at 3, 3
about - Pattern found  at 3, 4
about - Pattern found  at 3, 5
about - Pattern found  at 3, 6
about - Pattern found  at 3, 7
about - Pattern found  at 3, 8
about - Pattern found  at 3, 9
about - Pattern found  at 3, 10
about - Pattern found  at 4, 1
about - Pattern found  at 4, 2
about - Pattern found  at 4, 3
about - Pattern found  at 4, 4
about - Pattern found  at 4, 5
about - Pattern found  at 4, 6
about - Pattern found  at 4, 7
about - Pattern found  at 4, 8
about - Pattern found  at 4, 9
about - Pattern found  at 4, 10
about - Pattern found  at 5, 1
about - Pattern found  at 5, 2
about - Pattern found  at 5, 3
about - Pattern found  at 5, 4
about - Pattern found diag at 5, 5
about - Pattern found diag at 5, 6
about - Pattern found diag at 5, 7
about - Pattern found diag at 5, 8
about - Pattern found diag at 5, 9
about - Pattern found diag at 5, 10
about - Pattern found diag at 6, 1
about - Pattern found diag at 6, 2
about - Pattern found diag at 6, 3
about - Pattern found diag at 6, 4
about - Pattern found diag at 6, 5
about - Pattern found diag at 6, 6
about - Pattern found diag at 6, 7
about - Pattern found diag at 6, 8
about - Pattern found diag at 6, 9
about - Pattern found diag at 6, 10
about - Pattern found diag at 7, 1
about - Pattern found diag at 7, 2
about - Pattern found diag at 7, 3
about - Pattern found diag at 7, 4
about - Pattern found diag at 7, 5
about - Pattern found diag at 7, 6
about - Pattern found diag at 7, 7
about - Pattern found diag at 7, 8
about - Pattern found diag at 7, 9
about - Pattern found diag at 7, 10
about - Pattern found diag at 8, 1
about - Pattern found diag at 8, 2
about - Pattern found diag at 8, 3
about - Pattern found diag at 8, 4
about - Pattern found diag at 8, 5
about - Pattern found diag at 8, 6
about - Pattern found diag at 8, 7
about - Pattern found diag at 8, 8
about - Pattern found diag at 8, 9
about - Pattern found diag at 8, 10
about - Pattern found diag at 9, 1
about - Pattern found diag at 9, 2
about - Pattern found diag at 9, 3
about - Pattern found diag at 9, 4
about - Pattern found diag at 9, 5
about - Pattern found diag at 9, 6
about - Pattern found diag at 9, 7
about - Pattern found diag at 9, 8
about - Pattern found diag at 9, 9
about - Pattern found diag at 9, 10
about - Pattern found diag at 10, 1
about - Pattern found diag at 10, 2
about - Pattern found diag at 10, 3
about - Pattern found diag at 10, 4
about - Pattern found diag at 10, 5
about - Pattern found diag at 10, 6
about - Pattern found diag at 10, 7
about - Pattern found diag at 10, 8
about - Pattern found diag at 10, 9
about - Pattern found diag at 10, 10
above - Pattern found  at 1, 1
above - Pattern found horizontally at 1, 2
above - Pattern found horizontally at 1, 3
above - Pattern found horizontally at 1, 4
above - Pattern found horizontally at 1, 5
above - Pattern found horizontally at 1, 6
above - Pattern found horizontally at 1, 7
above - Pattern found horizontally at 1, 8
above - Pattern found horizontally at 1, 9
above - Pattern found horizontally at 1, 10
above - Pattern found horizontally at 2, 1
above - Pattern found horizontally at 2, 2
above - Pattern found horizontally at 2, 3
above - Pattern found horizontally at 2, 4
above - Pattern found horizontally at 2, 5
above - Pattern found horizontally at 2, 6
above - Pattern found horizontally at 2, 7
above - Pattern found horizontally at 2, 8
above - Pattern found horizontally at 2, 9
above - Pattern found horizontally at 2, 10
above - Pattern found horizontally at 3, 1
above - Pattern found horizontally at 3, 2
above - Pattern found horizontally at 3, 3
above - Pattern found horizontally at 3, 4
above - Pattern found horizontally at 3, 5
above - Pattern found horizontally at 3, 6
above - Pattern found horizontally at 3, 7
above - Pattern found horizontally at 3, 8
above - Pattern found horizontally at 3, 9
above - Pattern found horizontally at 3, 10
above - Pattern found horizontally at 4, 1
above - Pattern found horizontally at 4, 2
above - Pattern found horizontally at 4, 3
above - Pattern found horizontally at 4, 4
above - Pattern found horizontally at 4, 5
above - Pattern found horizontally at 4, 6
above - Pattern found horizontally at 4, 7
above - Pattern found horizontally at 4, 8
above - Pattern found horizontally at 4, 9
above - Pattern found horizontally at 4, 10
above - Pattern found horizontally at 5, 1
above - Pattern found horizontally at 5, 2
above - Pattern found horizontally at 5, 3
above - Pattern found horizontally at 5, 4
above - Pattern found horizontally at 5, 5
above - Pattern found horizontally at 5, 6
above - Pattern found horizontally at 5, 7
above - Pattern found horizontally at 5, 8
above - Pattern found horizontally at 5, 9
above - Pattern found horizontally at 5, 10
above - Pattern found horizontally at 6, 1
above - Pattern found horizontally at 6, 2
above - Pattern found horizontally at 6, 3
above - Pattern found horizontally at 6, 4
above - Pattern found horizontally at 6, 5
above - Pattern found horizontally at 6, 6
above - Pattern found horizontally at 6, 7
above - Pattern found horizontally at 6, 8
above - Pattern found horizontally at 6, 9
above - Pattern found horizontally at 6, 10
above - Pattern found horizontally at 7, 1
above - Pattern found horizontally at 7, 2
above - Pattern found horizontally at 7, 3
above - Pattern found horizontally at 7, 4
above - Pattern found horizontally at 7, 5
above - Pattern found horizontally at 7, 6
above - Pattern found horizontally at 7, 7
above - Pattern found horizontally at 7, 8
above - Pattern found horizontally at 7, 9
above - Pattern found horizontally at 7, 10
above - Pattern found horizontally at 8, 1
above - Pattern found horizontally at 8, 2
above - Pattern found horizontally at 8, 3
above - Pattern found horizontally at 8, 4
above - Pattern found horizontally at 8, 5
above - Pattern found horizontally at 8, 6
above - Pattern found horizontally at 8, 7
above - Pattern found horizontally at 8, 8
above - Pattern found horizontally at 8, 9
above - Pattern found horizontally at 8, 10
above - Pattern found horizontally at 9, 1
above - Pattern found horizontally at 9, 2
above - Pattern found horizontally at 9, 3
above - Pattern found horizontally at 9, 4
above - Pattern found horizontally at 9, 5
above - Pattern found horizontally at 9, 6
above - Pattern found horizontally at 9, 7
above - Pattern found horizontally at 9, 8
above - Pattern found horizontally at 9, 9
above - Pattern found horizontally at 9, 10
above - Pattern found horizontally at 10, 1
above - Pattern found horizontally at 10, 2
above - Pattern found horizontally at 10, 3
above - Pattern found horizontally at 10, 4
above - Pattern found horizontally at 10, 5
above - Pattern found horizontally at 10, 6
above - Pattern found horizontally at 10, 7
above - Pattern found horizontally at 10, 8
above - Pattern found horizontally at 10, 9
above - Pattern found horizontally at 10, 10
abuse - Pattern found  at 1, 1
abuse - Pattern found  at 1, 2
abuse - Pattern found  at 1, 3
abuse - Pattern found  at 1, 4
abuse - Pattern found  at 1, 5
abuse - Pattern found  at 1, 6
abuse - Pattern found  at 1, 7
abuse - Pattern found  at 1, 8
abuse - Pattern found  at 1, 9
abuse - Pattern found  at 1, 10
abuse - Pattern found  at 2, 1
abuse - Pattern found  at 2, 2
abuse - Pattern found  at 2, 3
abuse - Pattern found  at 2, 4
abuse - Pattern found  at 2, 5
abuse - Pattern found  at 2, 6
abuse - Pattern found  at 2, 7
abuse - Pattern found  at 2, 8
abuse - Pattern found  at 2, 9
abuse - Pattern found  at 2, 10
abuse - Pattern found  at 3, 1
abuse - Pattern found  at 3, 2
abuse - Pattern found  at 3, 3
abuse - Pattern found  at 3, 4
abuse - Pattern found  at 3, 5
abuse - Pattern found diag at 3, 6
abuse - Pattern found diag at 3, 7
abuse - Pattern found diag at 3, 8
abuse - Pattern found diag at 3, 9
abuse - Pattern found diag at 3, 10
abuse - Pattern found diag at 4, 1
abuse - Pattern found diag at 4, 2
abuse - Pattern found diag at 4, 3
abuse - Pattern found diag at 4, 4
abuse - Pattern found diag at 4, 5
abuse - Pattern found diag at 4, 6
abuse - Pattern found diag at 4, 7
abuse - Pattern found diag at 4, 8
abuse - Pattern found diag at 4, 9
abuse - Pattern found diag at 4, 10
abuse - Pattern found diag at 5, 1
abuse - Pattern found diag at 5, 2
abuse - Pattern found diag at 5, 3
abuse - Pattern found diag at 5, 4
abuse - Pattern found diag at 5, 5
abuse - Pattern found diag at 5, 6
abuse - Pattern found diag at 5, 7
abuse - Pattern found diag at 5, 8
abuse - Pattern found diag at 5, 9
abuse - Pattern found diag at 5, 10
abuse - Pattern found diag at 6, 1
abuse - Pattern found diag at 6, 2
abuse - Pattern found diag at 6, 3
abuse - Pattern found diag at 6, 4
abuse - Pattern found diag at 6, 5
abuse - Pattern found diag at 6, 6
abuse - Pattern found diag at 6, 7
abuse - Pattern found diag at 6, 8
abuse - Pattern found diag at 6, 9
abuse - Pattern found diag at 6, 10
abuse - Pattern found diag at 7, 1
abuse - Pattern found diag at 7, 2
abuse - Pattern found diag at 7, 3
abuse - Pattern found diag at 7, 4
abuse - Pattern found diag at 7, 5
abuse - Pattern found diag at 7, 6
abuse - Pattern found diag at 7, 7
abuse - Pattern found diag at 7, 8
abuse - Pattern found diag at 7, 9
abuse - Pattern found diag at 7, 10
abuse - Pattern found diag at 8, 1
abuse - Pattern found diag at 8, 2
abuse - Pattern found diag at 8, 3
abuse - Pattern found diag at 8, 4
abuse - Pattern found diag at 8, 5
abuse - Pattern found diag at 8, 6
abuse - Pattern found diag at 8, 7
abuse - Pattern found diag at 8, 8
abuse - Pattern found diag at 8, 9
abuse - Pattern found diag at 8, 10
abuse - Pattern found diag at 9, 1
abuse - Pattern found diag at 9, 2
abuse - Pattern found diag at 9, 3
abuse - Pattern found diag at 9, 4
abuse - Pattern found diag at 9, 5
abuse - Pattern found diag at 9, 6
abuse - Pattern found diag at 9, 7
abuse - Pattern found diag at 9, 8
abuse - Pattern found diag at 9, 9
abuse - Pattern found diag at 9, 10
abuse - Pattern found diag at 10, 1
abuse - Pattern found diag at 10, 2
abuse - Pattern found diag at 10, 3
abuse - Pattern found diag at 10, 4
abuse - Pattern found diag at 10, 5
abuse - Pattern found diag at 10, 6
abuse - Pattern found diag at 10, 7
abuse - Pattern found diag at 10, 8
abuse - Pattern found diag at 10, 9
abuse - Pattern found diag at 10, 10
added - Pattern found  at 1, 1
added - Pattern found  at 1, 2
added - Pattern found  at 1, 3
added - Pattern found  at 1, 4
added - Pattern found  at 1, 5
added - Pattern found  at 1, 6
added - Pattern found  at 1, 7
added - Pattern found  at 1, 8
added - Pattern found  at 1, 9
added - Pattern found  at 1, 10
added - Pattern found  at 2, 1
added - Pattern found  at 2, 2
added - Pattern found  at 2, 3
added - Pattern found  at 2, 4
added - Pattern found  at 2, 5
added - Pattern found  at 2, 6
added - Pattern found  at 2, 7
added - Pattern found  at 2, 8
added - Pattern found  at 2, 9
added - Pattern found  at 2, 10
added - Pattern found  at 3, 1
added - Pattern found  at 3, 2
added - Pattern found  at 3, 3
added - Pattern found  at 3, 4
added - Pattern found  at 3, 5
added - Pattern found  at 3, 6
added - Pattern found  at 3, 7
added - Pattern found  at 3, 8
added - Pattern found  at 3, 9
added - Pattern found  at 3, 10
added - Pattern found  at 4, 1
added - Pattern found  at 4, 2
added - Pattern found  at 4, 3
added - Pattern found  at 4, 4
added - Pattern found  at 4, 5
added - Pattern found  at 4, 6
added - Pattern found  at 4, 7
added - Pattern found  at 4, 8
added - Pattern found  at 4, 9
added - Pattern found  at 4, 10
added - Pattern found  at 5, 1
added - Pattern found  at 5, 2
added - Pattern found  at 5, 3
added - Pattern found  at 5, 4
added - Pattern found  at 5, 5
added - Pattern found  at 5, 6
added - Pattern found  at 5, 7
added - Pattern found  at 5, 8
added - Pattern found  at 5, 9
added - Pattern found  at 5, 10
added - Pattern found  at 6, 1
added - Pattern found  at 6, 2
added - Pattern found  at 6, 3
added - Pattern found  at 6, 4
added - Pattern found  at 6, 5
added - Pattern found  at 6, 6
added - Pattern found  at 6, 7
added - Pattern found  at 6, 8
added - Pattern found  at 6, 9
added - Pattern found  at 6, 10
added - Pattern found  at 7, 1
added - Pattern found  at 7, 2
added - Pattern found  at 7, 3
added - Pattern found  at 7, 4
added - Pattern found  at 7, 5
added - Pattern found  at 7, 6
added - Pattern found  at 7, 7
added - Pattern found  at 7, 8
added - Pattern found  at 7, 9
added - Pattern found  at 7, 10
added - Pattern found  at 8, 1
added - Pattern found  at 8, 2
added - Pattern found  at 8, 3
added - Pattern found  at 8, 4
added - Pattern found  at 8, 5
added - Pattern found  at 8, 6
added - Pattern found  at 8, 7
added - Pattern found  at 8, 8
added - Pattern found  at 8, 9
added - Pattern found  at 8, 10
added - Pattern found  at 9, 1
added - Pattern found  at 9, 2
added - Pattern found  at 9, 3
added - Pattern found  at 9, 4
added - Pattern found  at 9, 5
added - Pattern found  at 9, 6
added - Pattern found  at 9, 7
added - Pattern found  at 9, 8
added - Pattern found  at 9, 9
added - Pattern found  at 9, 10
added - Pattern found  at 10, 1
added - Pattern found  at 10, 2
added - Pattern found  at 10, 3
added - Pattern found  at 10, 4
added - Pattern found  at 10, 5
added - Pattern found  at 10, 6
added - Pattern found  at 10, 7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Main.printer(Main.java:67)
    at Main.patternSearch(Main.java:116)
    at Main.main(Main.java:160)

Ответы [ 2 ]

1 голос
/ 14 июля 2020

Насколько я понимаю, в вашем коде есть 2 проблемы.

  1. Операторы печати выполняются несколько раз.
  2. Исключение при поиске «добавлено».

Я буду объяснять одно за другим.

1.Печать операторов

Чтобы печатать оператор только тогда, когда найдено слово целиком, а не каждый раз, поместите оператор печати в предложение if, как показано ниже :

 if (search2D(grid, row, col, word)){
      mov=printer(grid, row,col,word);
      System.out.println(word +" - Pattern found " + mov +" at " + (row+1) + ", " + (col+1));
 }

Таким образом, печать выполняется только тогда, когда слово найдено.

2. Исключение

Это потому, что даже после достижения последней строки вы поиск следующей буквы в [row + 1]. Это бросает ArrayIndexOutOfBoundsException. Поэтому добавьте еще одну проверку к вашим операторам if в методе принтера, как показано ниже:

if((g+1)<10 && grid[g+1][h]==found){
    move="vertically";
    
}

else if((h+1)<10 && grid[g][h+1]==found){
    move="horizontally";
    
}

else if((g-1)>-1 && grid[g-1][h]==found){
    move="vertically";
    
}

else if((h-1)>-1 && grid[g][h-1]==found){
    move="horizontally";
    
}

else if((g-1)>-1 && (h-1)>-1 && grid[g-1][h-1]==found){
    move="diag";
    
}

else if((g+1)<10 && (h+1)<10 && grid[g+1][h+1]==found){
    move="diag";
    
}

else if((g-1)>-1 && (h+1)<10 && grid[g-1][h+1]==found){
    move="diag";

}
else if((h-1)>-1 && (g+1)<10 &&grid[g+1][h-1]==found){
    move="diag";

}

Таким образом, он будет проверять только ячейку, если она не превышает строк или столбцов сетки.

Пример вывода:

t   a   b   o   v   e   o   y   m   z   
s   u   q   o   e   a   v   i   u   s   
j   f   o   e   o   a   d   l   f   w   
t   h   r   b   q   f   b   u   x   z   
d   g   r   t   a   t   y   u   l   p   
a   w   c   s   n   e   t   d   s   t   
z   s   w   e   i   r   u   e   q   e   
q   v   g   g   a   x   l   d   z   z   
v   a   n   i   g   r   p   d   y   t   
c   u   d   j   a   w   d   a   n   k   

about - Pattern found diag at 5, 5
above - Pattern found horizontally at 1, 2
abuse - Pattern found diag at 3, 6
added - Pattern found vertically at 10, 8
adult - Pattern found diag at 2, 6
after - Pattern found vertically at 3, 6
again - Pattern found vertically at 10, 5
agent - Pattern found diag at 9, 2
agree - Pattern found diag at 6, 1
1 голос
/ 14 июля 2020

без фигурных скобок, оператор if применяется только к следующему оператору. вам следует добавить фигурные скобки, чтобы и printer(), и последующая печать в консоли выполнялись только в том случае, если search2D вернул true:

if (search2D(grid, row, col, word)) {
    mov = printer(grid, row, col, word);
    System.out.println(word + " - Pattern found " + mov + " at " + (row + 1) + ", " + (col + 1));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...