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.
时间: 2024-12-29 09:45:03

all possible patterns for N russian squares的相关文章

[BZOJ1717][Usaco2006 Dec]Milk Patterns 产奶的模式

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1297  Solved: 705 [Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". John的牛奶按质量可以被赋予一个0到100

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that

Partial least squares regression(偏最小二乘法回归)

偏最小二乘法(PLS)是近年来发展起来的一种新的多元统计分析 http://en.wikipedia.org/wiki/Partial_least_squares_regression Partial least squares regression(偏最小二乘法回归),布布扣,bubuko.com

设计模式(Design Patterns)

设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样.项目中合理的运用设计模式可以完美的解决很多问题,每种模式在现在中都有相应的原理来与之对应,每一个模式描述了一个在我们周围不断重复发生的问题,以及该问

HDU 1264 Counting Squares(模拟)

题目链接 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the lin

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s

Project Euler 98:Anagramic squares 重排平方数

Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively, we form a square number: 1296 = 362. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square num

Mining Diverse Patterns

Mining Diverse Patterns @(Pattern Discovery in Data Mining) Mining Diverse Patterns Mining Multi-level Association Rules Mining Multi-dimensional Associations Mining Quantitative Associations Mining Negative Correlations Mining Compressed Patterns Mi

uva201 Squares

 Squares  A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by the