Game of life (1) P26100


Statement
 

pdf   zip

The English mathematician John Conway invented in 1970 the following game: Imagine a matrix with nn rows and mm columns. We consider neighbor positions to a given position the (at most, eight) adjacent positions, either horizontally, vertically or diagonally. Every moment, each position is either empty or it contains a bacterium. The rules are:

  • An empty position at time tt will contain a bacterium at time t+1t + 1 if and only if at time tt it had exactly three neighbor bacteria.

  • An occupied position at time tt will contain a bacterium at time t+1t + 1 if and only if at time tt it had two or three neighbor bacteria.

Write a program that, for every given matrix, prints it at the next moment of time.

Input

Input consists of several cases. Every case begins with nn and mm (both strictly positive), followed by nn lines, each one with mm characters: ‘B’ if the position has a bacterium, and ‘.’ if the position is empty. A special case with n=m=0n = m = 0 marks the end of the input.

Output

For each case, print the matrix corresponding to the next moment of time using the same format of the input (do not print nn and mm). Separate matrices with an empty line.

Public test cases
  • Input

    2 3
    B.B
    .B.
    
    2 2
    BB
    BB
    
    0 0
    

    Output

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