We were given the following code to solve a Pentomino puzzle, several methods might need to be added or editted to solve the problem. The program should take in an input of 3 4 5 6 for 3x20 4x15 5x12 6x10 grids. Then output the number of correct solutions for them (respectively its 2, 368, 1010, and 2339)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

We were given the following code to solve a Pentomino puzzle, several methods might need to be added or editted to solve the problem. The program should take in an input of 3 4 5 6 for 3x20 4x15 5x12 6x10 grids. Then output the number of correct solutions for them (respectively its 2, 368, 1010, and 2339)

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class DLX {
    class DataNode {
        DataNode L, R, U, D;
        ColumnNode C;
        
        public DataNode() {
            L = R = U = D = this;
        }

        public DataNode(ColumnNode c) {
            this();
            C = c;
        }
        DataNode linkDown(DataNode node) {
            node.D = this.D;
            node.D.U = node;
            node.U = this;
            this.D = node;
            return node;
        }
        DataNode linkRight(DataNode node) {
            node.R = this.R;
            node.R.L = node;
            node.L = this;
            this.R = node;
            return node;
        }
        void linkLR() {
            this.L.R = this.R.L = this;
        }
        void unlinkLR() {
            this.L.R = this.R;
            this.R.L = this.L;
        }
        void linkUD() {
            this.U.D = this.D.U = this;
        }
        void unlinkUD() {
            this.U.D = this.D;
            this.D.U = this.U;
        }
    }

    class ColumnNode extends DataNode {
        int columnSize;
        String name;
        public ColumnNode(String name) {
            this.name = name;
            columnSize = 0;
            C = this;
        }
        void cover() {
            unlinkLR();
            for (DataNode i = this.D; i != this; i = i.D) {
                for (DataNode j = i.R; j != i; j = j.R) {
                    j.unlinkUD();
                    j.C.columnSize--;
                }
            }
        }
        void uncover() {
            for (DataNode i = this.U; i != this; i = i.U) {
                for (DataNode j = i.L; j != i; j = j.L) {
                    j.linkUD();
                    j.C.columnSize++;
                }
            }
            linkLR();
        }
    }

    private ColumnNode head;
    private List<ColumnNode> columnNodesList;
    private int solutions;
    private LinkedList<DataNode> solution;
    private DLX() {
        head = new ColumnNode("head");
        columnNodesList = new ArrayList<>();
        solution = new LinkedList<>();
    }
    public DLX(int numberOfCols, ArrayList<ArrayList<Integer>> matrix) {
        this();
        buildColumnHeaders(numberOfCols);
        buildDancingLinks(matrix);
    }
    public DLX(ArrayList<String> columnHeaders, ArrayList<ArrayList<Integer>> matrix) {
        this();
        buildColumnHeaders(columnHeaders);
        buildDancingLinks(matrix);
    }

    public void run() {
        solutions = 0;
        search(0);
    }
    public int getNumberOfSolutions() {
        return solutions;
    }
    private void search(int k) {

    }
    private ColumnNode selectColumn() {
        int min = Integer.MAX_VALUE;
        ColumnNode colNode = null;
        for (ColumnNode node = (ColumnNode) head.R; node != head; node = (ColumnNode) node.R) {
            if (node.columnSize < min) {
                min = node.columnSize;
                colNode = node;
            }
        }
        return colNode;
    }
    private void buildColumnHeaders(int numberOfCols) {
        ColumnNode temp = head;
        for (int i = 0; i < numberOfCols; i++) {
            ColumnNode node = new ColumnNode(Integer.toString(i));
            columnNodesList.add(node);
            temp = (ColumnNode) temp.linkRight(node);
        }
    }
    private void buildColumnHeaders(ArrayList<String> colHeaders) {
        int nCols = colHeaders.size();

        ColumnNode temp = head;
        for (int i = 0; i < nCols; i++) {
            ColumnNode node = new ColumnNode(colHeaders.get(i));
            columnNodesList.add(node);
            temp = (ColumnNode) temp.linkRight(node);
        }
    }

    private void buildDancingLinks(ArrayList<ArrayList<Integer>> matrix) {
        int nRows = matrix.size();
        for (int r = 0; r < nRows; r++) {
            DataNode temp = null;
            for (Integer i : matrix.get(r)) {
                ColumnNode colHead = columnNodesList.get(i);
                DataNode newNode = new DataNode(colHead);
                if (temp == null)
                    temp = newNode;
                colHead.U.linkDown(newNode);
                temp = temp.linkRight(newNode);
                colHead.columnSize++;
            }
        }
    }

    private void printSolution() {
        for (DataNode row : solution) {
            while (columnNodesList.indexOf(row.C) > 11)
                row = row.R;
            StringBuilder sb = new StringBuilder(row.C.name + " ");
            for (DataNode node = row.R; node != row; node = node.R) {
                sb.append(node.C.name + " ");
            }
            System.out.println(sb);
        }
        System.out.println();
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education