首页 > 代码库 > all possible patterns for N russian squares
all possible patterns for N russian squares
Question:
A traditional russion square is made by 4 blocks, and has 7 different patterns.
Given N blocks, how many different patterns would be.
N = 1, P# = 1
N = 2, P# = 1
N = 3, P# = 3
N = 4, P# = 7.
Assume N = k, P# = Pk
then when N = k+1, P# = for each P in Pk, append one more block around P, remove dups.
class Pattern { List<Block> blocks; // Return true if this pattern can be translated to another patter p by rotate or moving. boolean isSame(Pattern p) { // TODO } } List<Pattern> allPatthens(n) { if (n == 1) { return Pattern.builder().build(); } List<Pattern> patternsForNMinusOne = numOfPattern(n - 1); List<Pattern> toReturn = new ArrayList<>(); for (Pattern p : patternsForNMinus1) { List<Pattern> patternsWithOneMoreBlock = patternsWithOneMoreBlock(p); for (Pattern newP : patternsWithOneMoreBlock) { if (!contains(toReturn, newP)) { toReturn.add(newP); } } } return toReturn; } boolean contains(List<Pattern> list, Pattern p) { for (Pattern pInList : list) { if (pInList.isSame(p)) return true; } return false; } // Given pattern p. // Return all unique valid patterns with one more block. List<Pattern> patternsWithOneMoreBlock(Pattern p) { List<Pattern> toReturn = new ArrayList<>(); for (Block b : p.blocks()) { List<Block> newBlocks = Arrays.as(b.left(), b.right(), b.up(), b.down()); for (Block newBlock : newBlocks) { if (!p.contains(newBlock)) { // A new pattern found Pattern newP = p.appendBlock(newBlock); if (!contains(toReturn, newP)) { toReturn.add(newP); } } } } return toReturn; } // Too complicated.
all possible patterns for N russian squares
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。