amazon 设计 10 puzzle.

本文介绍了一种解决拼图游戏的算法实现。该算法通过区分拼图边框的不同类型(内侧、外侧和平面),逐步从角落开始向内解决整个拼图。文章详细展示了如何对拼图的各个部分进行分类,并通过匹配内外侧边来逐步构建完整的解决方案。
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.
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/leetcode/p/3906335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值