Game of the life (2) P27283


Statement
 

pdf   zip

html

This exercise is a continuation of the exercise P26100: “Game of life (1)”

Let M0 be a matrix with bacteria at the initial time, and let M1, M2, M3, …be the matrices at the times 1, 2, 3, …Write a program that, given M0, finds the cycle that is obtained starting at M0, that is, the first and shortest sequence of matrices Mi, Mi+1, …, Mj−1, Mj such that Mj+1 = Mi. Suppose j < 100.

Input

Input consists of the description of the matrix M0: two strictly positive natural numbers n and m, followed by n lines, each one with m characters: ‘B’ if the position has a bacterium, and ‘.’ if the position is empty.

Output

Print the matrices of the cycle Mi, Mi+1, …, Mj−1, Mj separated by an empty line.

Public test cases
  • Input

    7 7
    ....BBB
    .B.BBBB
    .B.BBBB
    ..BBBBB
    .B.BBBB
    .B.BBBB
    ....BBB
    

    Output

    .......
    .......
    .......
    BBB....
    .......
    .......
    .......
    
    .......
    .......
    .B.....
    .B.....
    .B.....
    .......
    .......
    
  • Input

    2 2
    BB
    ..
    

    Output

    ..
    ..
    
  • Input

    10 10
    ..........
    ...BBBB...
    ...B..B...
    .BBB..BBB.
    .B......B.
    .B......B.
    .BBB..BBB.
    ...B..B...
    ...BBBB...
    ..........
    

    Output

    ..........
    ...BBBB...
    ...B..B...
    .BBB..BBB.
    .B......B.
    .B......B.
    .BBB..BBB.
    ...B..B...
    ...BBBB...
    ..........
    
    ....BB....
    ...BBBB...
    ..........
    .B.B..B.B.
    BB......BB
    BB......BB
    .B.B..B.B.
    ..........
    ...BBBB...
    ....BB....
    
    ...B..B...
    ...B..B...
    ..BB..BB..
    BBB....BBB
    ..........
    ..........
    BBB....BBB
    ..BB..BB..
    ...B..B...
    ...B..B...
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++ Java Python
    User solutions
    C++