(UVA) Where's Waldorf? - Solution 1

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=951

Given a grid, we need to find the first occurrence of a specific word (if it exists). Then, it is possible to use a Depth-First Search to find the answer to this problem. Each iteration in the mentioned method will try to find the next character related to the searched word.


import java.io.*;
import java.util.*;

class Main {
    public int numRows;
    public int numCols;
    public String word;
    public boolean gotWord;
    public char[][] matrix;
    public int[] vi = {-1,-1,-1,0,0,1,1,1};
    public int[] vj = {-1,0,1,-1,1,-1,0,1};
   
    public void dfs(int currI, int currJ, int currIndexWord, int straight) {
        if (currI < 0 || currJ < 0 || currI >= numRows || currJ >= numCols || matrix[currI][currJ] != word.charAt(currIndexWord)) {
            return;
        }
        if (currIndexWord == word.length()-1) {
            gotWord = true;
            return;
        }

        dfs(currI+vi[straight], currJ+vj[straight], currIndexWord+1, straight);
    }
   
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        int numTests = sc.nextInt();
        for (int i = 0; i < numTests; i++) {
            if (i > 0) {
                System.out.println();
            }
            numRows = sc.nextInt();
            numCols = sc.nextInt();
            matrix = new char[numRows][numCols];
            for (int j = 0; j < numRows; j++) {
                String line = sc.next();
                line = line.toUpperCase();
                for (int k = 0; k < numCols; k++) {
                    matrix[j][k] = line.charAt(k);
                }
            }
           
            int numWords = sc.nextInt();
            for (int j = 0; j < numWords; j++) {
                word = sc.next();
                word = word.toUpperCase();
                gotWord = false;
                for (int k = 0; k < numRows && !gotWord; k++) {
                    for (int l = 0; l < numCols && !gotWord; l++) {
                        if (matrix[k][l] != word.charAt(0)) {
                            continue;
                        }
                        for (int m = 0; m < 8 && !gotWord; m++) {
                            dfs(k+vi[m], l+vj[m], 1, m);
                            if (gotWord) {
                                System.out.println((k+1) + " " + (l+1));
                            }
                        }
                    }
                }
            }
        }
                                       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução