Requirement
Given N numbers of jigsaw puzzle pieces, design a solution to solve this puzzle.
We don’t know the length or width of the puzzle.
Each piece may not put in the correct angle, may need rotation to solve it.
Each edge of a piece only has 1 other edge can match it.
isMatched(Edge e) will return true if two edges match.
class Piece {
Edge top;
Edge bot;
Edge left;
Edge right;
int finalDegree;
}
class Edge{
enum Type{
INNER, OUTER, FLAT
}
Type type;
boolean isMatched(Edge edge) {}
}
class Puzzle {
Piece[][] result;
Edge[] inners;
Edge[] outers;
Edge[] flats;
Edge exposed_edge;
void rotatePiece(String curDirection, String tarDirection, Piece p) {
// rotate piece to target direction, set finalDegree
}
void solve() {
// find one corner piece
// try to solve one row by iteration started from 1 edge of the corner piece
// use isMatched() to find next edge & piece
// use rotatePiece() rotate piece to correct degree/angle
// store the other exposed edge of corner piece to exposed_edge
// repeat above iteration to solve row by row
}
}
Reference
https://massivetechinterview.blogspot.com/2015/07/implement-jigsaw-puzzle-kodeknight.html