-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
76 lines (52 loc) · 1.9 KB
/
WordSearch.java
File metadata and controls
76 lines (52 loc) · 1.9 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
import java.io.*;
/// Given 2D array with alphabets
/// Search the given word from the grid
/// travelling across the grid can be done by moving - up, down,left, right.
public class WordSearch{
static int[] rows = {-1,1,0,0};
static int[] col = {0,0,-1,1};
static boolean recurse(char[][] grid, String word, int n, int i, int r, int c){
if(i >= word.length()) return false;
if(r<0 || c<0 || r>=n || c >=n) return false;
if(grid[r][c] != word.charAt(i)) return false;
/// The word is found.
if(i==word.length()-1) return true;
/// Marking visited node
grid[r][c] = '$';
/// recursive intuition
boolean retval = false;
for(int k=0; k<4; k++){
retval = recurse(grid, word, n, i+1,r+rows[k], c+col[k]);
/// retval will hold true when the word is found.
if(retval) break;
}
/// Mark the node as not visited for next iteration as word is not found.
grid[r][c] = word.charAt(i);
return retval;
}
static boolean find(char[][] grid, String word, int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(grid[i][j] == word.charAt(0)){
if(recurse(grid, word, n, 0, i, j)) return true;
}
}
}
return false;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n,i,j;
System.out.println("Size of grid: ");
n = sc.nextInt();
char[][] chars = new char[n][n];
for(i=0; i<n; i++){
for(j=0; j<n; j++) chars[i][j] = sc.next().charAt(0);
}
String word = sc.next();
/// Output true is word is found and false if not found.
System.out.println(find(chars, word, n));
sc.close();
}
}