-1
public class coggle {

 public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    HashSet<String> d = new HashSet<>();
    String[][] board = new String[5][5];

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            board[i][j] = scan.next();
        }
        scan.nextLine();
    }
    String next = "";
    while (scan.hasNextLine()) {
        next = scan.nextLine();
        if ("done".equals(next)) {
            break;
        }
        d.add(next);
    }
    boolean[][] visited = new boolean[5][5];
    ArrayList<String> s = new ArrayList<>();

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            f(i, j, board, visited, "", d, s);
        }
    }  
}

public static void f(int r, int c, String[][] board, boolean[][] visited, String s, HashSet<String> d, ArrayList<String> words) {
    if (r >= 5 || r < 0) {
        return;
    }if (c >= 5 || c < 0) {
        return;
    }if (visited[r][c]) {
        return;
    }
    s += board[r][c];
    if (d.contains(s) && !words.contains(s)) {
        words.add(s);
        System.out.println(s);
    }
    visited[r][c] = true;

    for (int i = -1; i < 2; i++) {
        for (int j = -1; j < 2; j++) {
            f(r + i, c + j, board, visited, s, d, words);
        }
    }
  }
}

Basically, I just want the program to print out any words that are found. It never prints any words, however.

It is supposed to work on a 5x5 board, and and the input for the hashset dictionary stops when I type "done".

Raju
  • 2,902
  • 8
  • 34
  • 57
  • I'm not sure what is your problem, I got the algorithm to print some words. What are you using as input data? – f-CJ Jul 28 '18 at 23:04

1 Answers1

1

I don't see anything wrong with the algorithm so it may be the input data that is making the algorithm not to print any word. I modified the code to hard code the input data instead of retrieving it from System.in.

Board input:

b | a | r | s | z
z | z | z | z | z
z | z | z | z | z
z | z | z | z | z
z | z | z | z | z

Dictionary input: bar, bars

public class Coogle {

    public static void main(String[] args) {
        HashSet<String> d = new HashSet<>(Arrays.asList("bar", "bars"));
        String[][] board = {{"b", "a", "r", "s", "z"}, {"z", "z", "z", "z", "z"}, {"z", "z", "z", "z", "z"}, {"z", "z", "z", "z", "z"}, {"z", "z", "z", "z", "z"}};

        boolean[][] visited = new boolean[5][5];
        ArrayList<String> s = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                f(i, j, board, visited, "", d, s);
            }
        }
    }

    public static void f(int r, int c, String[][] board, boolean[][] visited, String s, HashSet<String> d, ArrayList<String> words) {
        if (r >= 5 || r < 0) {
            return;
        }
        if (c >= 5 || c < 0) {
            return;
        }

        if (visited[r][c]) {
            return;
        }

        s += board[r][c];

        if (d.contains(s) && !words.contains(s)) {
            words.add(s);
            System.out.println(s);
        }

        visited[r][c] = true;

        for (int i = -1; i < 2; i++) {
            for (int j = -1; j < 2; j++) {
                f(r + i, c + j, board, visited, s, d, words);
            }
        }

    }
}

The algorithm is printing bar and bars

f-CJ
  • 4,235
  • 2
  • 30
  • 28
  • My problem is not input; I have printed the input back to the system to make sure this isn't the case. My problem is that the program only detects some words and not others.On this board:Z C C D X K Q M N B U O W Z Y F C O I J P A Q Z T it only detects "zoo". This has something to do with the way I make visited[r][c] true and false, but I am not completely sure why. – Avi Gupta Jul 29 '18 at 21:39
  • @AviGupta you should point out which words are in the dictionary and the ones not being detected, given an specific board. In the comment you are specifying the board, but not the whole dictionary nor which words are not being detected. – f-CJ Jul 29 '18 at 21:44