Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean solveDFS(Integer[][] matrix, int i, int j) {
- if (i == dimension - 1 && j >= dimension) return true;
- if (j == dimension) {
- System.out.println(convertGameToShow(matrix));
- return solveDFS(matrix.clone(), i + 1, 0);
- }
- if (matrix[i][j] != -1) {
- return solveDFS(matrix.clone(), i, j + 1);
- } else {
- for (int num = 1; num <= dimension; num++) {
- Integer[][] aux = matrix.clone();
- aux[i][j] = num;
- if (check(aux, i, j)) {
- matrix[i][j] = num;
- if (solveDFS(matrix.clone(), i, j + 1)) {
- gameMatrix = matrix;
- return true;
- }
- }
- }
- matrix[i][j] = -1;
- }
- return false;
- }
- public boolean check(Integer[][] game, int i, int j) {
- ArrayList<Integer> lineVal = new ArrayList<>();
- ArrayList<Integer> colVal = new ArrayList<>();
- for (int k = 0; k < dimension; k++) {
- if (game[i][k] != -1 && lineVal.contains(game[i][k]))
- return false;
- else lineVal.add(game[i][k]);
- if (game[k][j] != -1 && colVal.contains(game[k][j]))
- return false;
- else colVal.add(game[k][j]);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement