Boggle ( Find all possible words in a board of characters ) | Set 1 | Java program for Boggle game ; Let the given dictionary be following ; A given function to check if a given string is present in dictionary . The implementation is naive for simplicity . As per the question dictionary is given to us . ; Linearly search all words ; A recursive function to print all words present on boggle ; Mark current cell as visited and append current character to str ; If str is present in dictionary , then print it ; Traverse 8 adjacent cells of boggle [ i ] [ j ] ; Erase current character from string and mark visited of current cell as false ; Prints all words present in dictionary . ; Mark all characters as not visited ; Initialize current string ; Consider every character and look for all words starting with this character ; Driver program to test above function
class GFG { static final String dictionary [ ] = { " GEEKS " , " FOR " , " QUIZ " , " GUQ " , " EE " } ; static final int n = dictionary . length ; static final int M = 3 , N = 3 ; static boolean isWord ( String str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . equals ( dictionary [ i ] ) ) return true ; return false ; } static void findWordsUtil ( char boggle [ ] [ ] , boolean visited [ ] [ ] , int i , int j , String str ) { visited [ i ] [ j ] = true ; str = str + boggle [ i ] [ j ] ; if ( isWord ( str ) ) System . out . println ( str ) ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row ] [ col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str = " " + str . charAt ( str . length ( ) - 1 ) ; visited [ i ] [ j ] = false ; } static void findWords ( char boggle [ ] [ ] ) { boolean visited [ ] [ ] = new boolean [ M ] [ N ] ; String str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } public static void main ( String args [ ] ) { char boggle [ ] [ ] = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; System . out . println ( " Following ▁ words ▁ of ▁ dictionary ▁ are ▁ present " ) ; findWords ( boggle ) ; } }