(UVA) Where's Waldorf? - Solution 2

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


This solution is very similar to the previous one, posted yesterday. If you want to see the other solution, click here.

The difference between this solution and the previous one is that here it is not being used a recursive method, but we use a loop that try to find the next character related to the searched word in each iteration.


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 findWord(int currI, int currJ, int currIndexWord, int straight) {
        for (int i = 0; i < Math.max(numRows, numCols); i++) {
            if (currI < 0 || currJ < 0 || currI >= numRows || currJ >= numCols || matrix[currI][currJ] != word.charAt(currIndexWord)) {
                return;
            }
            if (currIndexWord == word.length()-1) {
                gotWord = true;
                return;
            }

            currI += vi[straight];
            currJ += vj[straight];
            currIndexWord++;
        }
    }
   
    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;
                        }
                        if (word.length() == 1) {
                            System.out.println((k+1) + " " + (l+1));
                            gotWord = true;
                        }
                        for (int m = 0; m < 8 && !gotWord; m++) {
                            findWord(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) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução