Backtracking Word Puzzle Alice Has Made A Nx N Grid Of Characters She Has M Words With Her A Word Is Said To Be Prese 1 (102.22 KiB) Viewed 29 times
Backtracking Word Puzzle Alice Has Made A Nx N Grid Of Characters She Has M Words With Her A Word Is Said To Be Prese 2 (50.53 KiB) Viewed 29 times
Backtracking Word Puzzle Alice Has Made A Nx N Grid Of Characters She Has M Words With Her A Word Is Said To Be Prese 3 (88.41 KiB) Viewed 29 times
Backtracking: Word Puzzle Alice has made a Nx N grid of characters. She has M words with her. A word is said to be present in the grid if it can be formed from a sequence of characters that are either horizontally or vertically or diagonally adjacent. Find the number of words that are present in the grid. Note: For a word, you cannot use a character at a position more than once. Function Description: In the provided code snippet, implement the provided formWords (...) method using the variables to print the number of words that are present in the grid. You can write your code in the space below the phrase "WRITE YOUR LOGIC HERE". There will be multiple test cases running so the Input and Output should match exactly as provided. The base Output variable result is set to a default value of -404 which can be modified. Additionally, you can add or remove these output variables. Input Format The first line of input contains two integers N and M. The next N lines will contain N space-separated lowercase characters stored in a matrix alpha. The next line contains M space-separated strings stored in a string array words. Sample Input 43 dtth --denotes N and M. --denotes N space-separated
sgqt characters. zety characters. dvgj characters. as hqe sdtth --denotes N space-separated lowercase --denotes N space-separated lowercase --denotes N space-separated lowercase --denotes M space-separated strings. Constraints 1 <= N <= 10, 1 <= M <= 100 1 <= words.length() <= 100 Output Format The output contains a single integer denoting the total number of words that are present in the grid. Sample Output 2 Explanation The first word "as" cannot be formed as "a" is not present in the grid. The second word "hqe" can be formed by sequentially using the characters at positions (0,3), (1,2), and (2,1). The third word "sdtth" can be formed by sequentially using the characters at positions (1,0), (0,0), (0,1), (0,2), and (0,3). Hence, 2 words can be formed using the grid.
56789 10 11 12 13 12 14 15 PPP 16 17 18 19 20 21 22 23 24 w w w w w w w w w N N N N N N N 25 26 27 28 29 30 31 32 234 in 33 34 35 36 679 37 public class Main{ public static int formWords (int n, int M, String[] [] alpha, String[] words) { //this is default OUTPUT. You can change it. int result = -404; 38 } } //write your Logic here: return result; public static void main (String[] args) { Scanner sc = new Scanner(System.in); // INPUT [uncomment & modify if required] int N = sc.nextInt (); int M = sc.nextInt (); String[] [] alpha = new String [N] [N]; for(int i=0; i<n; i++) { } for(int j = 0; j<N; j++){ alpha [j] =sc.next(); } } String[] words = new String [M]; for (int i = 0; i < M; i++) { T words = sc.next(); } sc.close(); System.out.print(formWords (N, M, alpha, words));
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!