2014-04-23 22:57
题目:实现一个数据结构来表示拼图游戏中的碎片。
解法:一个拼图块儿有四条边,每边只有凹凸平三种情况,当两块碎片拼接的时候,分为四个方向进行,块儿上的图案肯定也是判断是否能拼接的依据之一。所以就有了以下的表示方法,又一题做得云里雾里。话说这种题目在面试时如果真遇到,得写到什么程度的代码才算及格?
代码:
1 // 8.6 Design data structure to represent pieces in jigsaw puzzle. If possible please design an algorithm to solve it. 2 // I'll use the struct below to represent a piece, which has four sides, each one of which is either concave, convex or plain. 3 // And they must have some patterns on them, which can be considered as images. 4 // Solving a jigsaw can be done with DFS, which can be O(n!) in time scale, where n is total number of pieces. 5 // A possibl way is to do it in diagonal manner, starting from the left top corner. 6 // Like this: 7 // 0 1 2 3 8 // 1 2 3 . 9 // 2 3 ... 10 // 3 ..... 11 // When doing the search, you have to check if a piece can be fit into a targetted position. 12 // a method fitsWith() will be used, parameters will included two pieces and a direction. 13 // For example, fitsWith(piece1, piece2, BOTTOM) means if piece2 fits on the bottom of piece1. 14 struct JigsawPuzzlePiece { 15 // left, top, right, bottom 16 // -1 for concave, +1 for convex, 0 for plain 17 int side[4]; 18 Image *image; 19 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)