-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBoggle.java
More file actions
57 lines (48 loc) · 1.84 KB
/
Boggle.java
File metadata and controls
57 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Java program for Boggle game
class Boggle {
// Let the given dictionary be following
static final String[] dictionary = { "GEEKS", "FOR", "QUIZ", "GUQ", "EE" };
static final int n = dictionary.length;
static final int M = 3, N = 3;
// 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.
static boolean isWord(String str)
{
// Linearly search all words
for (int i = 0; i < n; i++)
if (str.equals(dictionary[i]))
return true;
return false;
}
// A recursive function to print all words present on boggle
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);
}
// Driver program to test above function
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);
}
}