《Cracking the Coding Interview》——第8章:面向对象设计——题目6
2014-04-23 23:30
501 查看
2014-04-23 22:57
题目:实现一个数据结构来表示拼图游戏中的碎片。
解法:一个拼图块儿有四条边,每边只有凹凸平三种情况,当两块碎片拼接的时候,分为四个方向进行,块儿上的图案肯定也是判断是否能拼接的依据之一。所以就有了以下的表示方法,又一题做得云里雾里。话说这种题目在面试时如果真遇到,得写到什么程度的代码才算及格?
代码:
题目:实现一个数据结构来表示拼图游戏中的碎片。
解法:一个拼图块儿有四条边,每边只有凹凸平三种情况,当两块碎片拼接的时候,分为四个方向进行,块儿上的图案肯定也是判断是否能拼接的依据之一。所以就有了以下的表示方法,又一题做得云里雾里。话说这种题目在面试时如果真遇到,得写到什么程度的代码才算及格?
代码:
// 8.6 Design data structure to represent pieces in jigsaw puzzle. If possible please design an algorithm to solve it. // I'll use the struct below to represent a piece, which has four sides, each one of which is either concave, convex or plain. // And they must have some patterns on them, which can be considered as images. // Solving a jigsaw can be done with DFS, which can be O(n!) in time scale, where n is total number of pieces. // A possibl way is to do it in diagonal manner, starting from the left top corner. // Like this: // 0 1 2 3 // 1 2 3 . // 2 3 ... // 3 ..... // When doing the search, you have to check if a piece can be fit into a targetted position. // a method fitsWith() will be used, parameters will included two pieces and a direction. // For example, fitsWith(piece1, piece2, BOTTOM) means if piece2 fits on the bottom of piece1. struct JigsawPuzzlePiece { // left, top, right, bottom // -1 for concave, +1 for convex, 0 for plain int side[4]; Image *image; };
相关文章推荐
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目2
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目3
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目4
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目10
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目5
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目1
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目1
- 《Cracking the Coding Interview》——第2章:链表——题目7
- 《Cracking the Coding Interview》——第4章:树和图——题目2
- 《Cracking the Coding Interview》——第5章:位操作——题目5
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目2
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
- 程序员面试题目:Cracking the coding interview 分析与解答
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目6
- 《Cracking the Coding Interview》——第12章:测试——题目2
- 《Cracking the Coding Interview》——第13章:C和C++——题目7
- 《Cracking the Coding Interview》——第17章:普通题——题目9