(UVA) The Dominoes Solitaire - Solution

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

For each domino, it is necessary to check if one of its sides matches with one of the sides of the borders. If a match is found, that border will become the new domino. This process is repeated until the number of dominoes required be reached or until it finds out that it is not possible to find a solution. The approach used was backtracking.


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

class Main {
    public static int numSpaces;
    public static int numPieces;
    public static ArrayList<Domino> dominos;
    public static int[] considered;
    public static boolean gotMatch;
    public static boolean gotAnswer;
   
    public static void rec(int leftRight, int rightLeft, int spaces) {
        if (spaces == 0 && leftRight == rightLeft) { // left and right side must match to have only one sequence of dominoes
            gotAnswer = true;
            return;
        }
        if (gotAnswer) {
            return;
        }
   
        for (int i = 0; i < dominos.size(); i++) {
            if (considered[i] == 1) {
                continue;
            }
           
            int newLeftRight = leftRight;
            int newRightLeft = rightLeft;
            gotMatch = false;
           
            if (dominos.get(i).left == leftRight) {
                newLeftRight = dominos.get(i).right;
                gotMatch = true;
            }
            else if (dominos.get(i).right == leftRight) {
                newLeftRight = dominos.get(i).left;
                gotMatch = true;
            }
            else if (dominos.get(i).left == rightLeft) {
                newRightLeft = dominos.get(i).right;
                gotMatch = true;
            }
            else if (dominos.get(i).right == rightLeft) {
                newRightLeft = dominos.get(i).left;
                gotMatch = true;
            }
            if (gotMatch) {
                considered[i] = 1; // usar um vetor
                rec(newLeftRight, newRightLeft, spaces-1);
                considered[i] = 0;
            }
        }
    }
   
    public static void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        numSpaces = sc.nextInt();
        while (numSpaces != 0) {
            numPieces = sc.nextInt();
            dominos = new ArrayList<>();
           
            int l = sc.nextInt();
            int r = sc.nextInt();
            Domino left = new Domino(l, r);
            l = sc.nextInt();
            r = sc.nextInt();
            Domino right = new Domino(l, r);
           
            for (int i = 0; i < numPieces; i++) {
                l = sc.nextInt();
                r = sc.nextInt();
                dominos.add(new Domino(l, r));
            }
           
            gotAnswer = false;
            considered = new int[numPieces];
            rec(left.right, right.left, numSpaces);
            if (gotAnswer) {
                System.out.println("YES");
            }
            else {
                System.out.println("NO");
            }
           
            numSpaces = sc.nextInt();
        }
                                       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

class Domino {
    int left;
    int right;
   
    public Domino(int l, int r) {
        left = l;
        right = r;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução