(UVA) Playing Boggle - Solution

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

We need to iterate over all the board in order to find if there is a way to form each given word. The solution below used backtracking to solve this problem.


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

class Main {
    public char[][] matrix;
    public boolean[][] visited;
    public String word;
    public boolean gotWord;
    public int[] vi = {-1,-1,-1,0,0,1,1,1};
    public int[] vj = {-1,0,1,-1,1,-1,0,1};
    public HashMap<Integer, Integer> mapScore;
   
    public void initMapScore() {
        mapScore.put(3, 1);
        mapScore.put(4, 1);
        mapScore.put(5, 2);
        mapScore.put(6, 3);
        mapScore.put(7, 5);
    }
   
    public void rec(int currI, int currJ, int currIndexWord) {
        if (currI < 0 || currJ < 0 || currI >= 4 || currJ >= 4 || visited[currI][currJ] || matrix[currI][currJ] != word.charAt(currIndexWord)) {
            return;
        }
        if (currIndexWord == word.length()-1) {
            gotWord = true;
            return;
        }
       
        for (int i = 0; i < 8; i++) {
            int nextI = currI+vi[i];
            int nextJ = currJ+vj[i];
            visited[currI][currJ] = true;
            rec(nextI, nextJ, currIndexWord+1);
            visited[currI][currJ] = false;
        }
    }
   
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        mapScore = new HashMap<>();
        initMapScore();
       
        int numTests = sc.nextInt();
        for (int i = 0; i < numTests; i++) {
            matrix = new char[4][4];
            for (int j = 0; j < 4; j++) {
                String line = sc.next();
                for (int k = 0; k < 4; k++) {
                    matrix[j][k] = line.charAt(k);
                }           
            }
                       
            int countPoints = 0;
            int numWords = sc.nextInt();
            for (int j = 0; j < numWords; j++) {
                word = sc.next();
                gotWord = false;
                visited = new boolean[4][4];
                for (int k = 0; k < 4 && !gotWord; k++) {
                    for (int l = 0; l < 4 && !gotWord; l++) {
                        if (matrix[k][l] == word.charAt(0)) {
                            rec(k, l, 0);
                            if (!gotWord) {
                                continue;
                            }
                            if (word.length() >= 8) {
                                countPoints += 11;
                                continue;
                            }
                            countPoints += mapScore.get(word.length());
                        }  
                    }
                }
            }
            System.out.println("Score for Boggle game #" + (i+1) + ": " + countPoints);
        }
                                       
        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