// Tyler Mi // Showing an application of recursion, brute-force exploring // of possibilities import java.util.*; public class RecursiveBacktracking { public static void main(String[] args) { fourAB(); diceRoll(2); } // Prints all strings of length 4 consisting of a's and b's public static void fourAB() { fourABHelper(""); } // Prints all strings of length 4 consisting of a's and b's // that start with the given prefix "accumulator" private static void fourABHelper(String accumulator) { if (accumulator.length() == 4) { System.out.println(accumulator); } else { fourABHelper(accumulator + "a"); fourABHelper(accumulator + "b"); } } // Assumes that n >= 0 // Prints all possible outcomes of rolling the given number of // 6-sided dice in the format [#, #, #] public static void diceRoll(int n) { diceRollHelper(new ArrayList<>(), n); } // prints all possible outcomes of rolling the given number of // 6-sided dice in the format [#, #, #] with chosen as the rolls // that have already been computed private static void diceRollHelper(List chosen, int remaining) { if (remaining == 0) { // Success System.out.println(chosen); } else { // In-Bounds for (int i = 1; i <= 6; i++) { // for choice we have chosen.add(i); // choose diceRollHelper(chosen, remaining - 1); // explore chosen.remove(chosen.size() - 1); // unchoose } } } }