Thursday, July 30, 2015

Implement a jigsaw puzzle ~ KodeKnight



Implement a jigsaw puzzle ~ KodeKnight
Implement a jigsaw puzzle. Design the data structures and explain an algorithm to solve the puzzle.
In jigsaw puzzles we take the piece, and make a meaningfull things from the set of pieces given to us.

and we can have a meaning full picture from above pieces :


We will need couple of classes to represent this:
  • Piece
  • Edge (contained by Piece)
  • Jigsaw game
First you need a "Piece" class to represent one piece of a puzzle. Each piece has four sides, each one with a unique outline which will only connect to one other piece.

Edge sides have an "edge" outline. Each side also has a piece id attribute called "adjacent" to store the value of the piece it connects to. Piece provides a "rotate" method which turns the piece 90, 180, or 270 degrees.

The "Jigsaw" class has a number of pieces in the puzzle and a container of pieces. The "Solve" method uses a map to store the association of edges to pieces. It iterates through the pieces and for each edge, looks to see if its compliment is in the map. If so, it rotates the new piece to the correct orientation and sets the adjacent fields in the two edges to point at each other. If not, it adds the edge to the map. With one pass through the pieces, it should have all the pieces in the correct orientation and connected to all of the adjacent pieces.
class Edge {
    enum Type {
        inner, outer, flat
    }
  
    Piece parent;
    Type type;
  
    boolean fitsWith(Edge type) {
    }; // Inners & outer fit together.
}
  
class Piece {
    Edge left, right, top, bottom;
  
    // 90, 180, etc
    Orientation solvedOrientation = 90;
}
  
class Puzzle {
    // Remaining pieces left to put away.
    Piece[][] pieces;
    Piece[][] solution;
    Edge[] inners, outers, flats;
  
    // We're going to solve this by working our way
    // in-wards, starting with the corners.
    // This is a list of the inside edges.
    Edge[] exposed_edges;
  
    void sort() {
  
        // Iterate through all edges,
        // adding each to inners, outers, etc,
        // as appropriate.
        // Look for the corners—add those to solution.
        // Add each non-flat edge of the corner
        // to exposed_edges.
  
    }
  
    void solve() {
        for (Edge edge1 : exposed_edges) {
            // Look for a match to edge1
            if (edge1.type == Edge.Type.inner) {
                for (Edge edge2 : outers) {
                    if (edge1.fitsWith(edge2)) {
                        // We found a match!
                        // Remove edge1 from
                        // exposed_edges.
                        // Add edge2's piece
                        // to solution.
                        // Check which edges of edge2
                        // are exposed, and add
                        // those to exposed_edges.
                    }
                }
                // Do the same thing,
                // swapping inner & outer.
            }
        }
    }
}
Read full article from Implement a jigsaw puzzle ~ KodeKnight

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts