The game of the life P30322


Statement
 

pdf   zip

The game of the life

The english mathematician John Conway invented in 1970 the following game:

Imagine a matrix with nn rows and mm columns. The (at most, eight) positions which are at distance 1, horizontally, vertically or in diagonal are considered adjacent positions. In each moment, each position of the matrix is empty or contains a tree. The rules are:

  • An empty position in a moment tt will contain a tree at the moment t+1t + 1 if and only if at the moment tt had exactly three adjacent trees.

  • An occupied position at the moment tt will contain a tree at the moment t+1t + 1 if and only if at the moment tt had two or three adjacent trees.

Your task is to write a program that, for each given matrix, prints the matrix at the posterior moment of time.

Input

The input consists of zero or more cases. Each case consists of a line with NN and MM (two integers between 1 and 100) followed by NN lines (one per row) each one with MM characters: X if the position is occupied and . if the position is empty. A line with N=M=0N = M = 0 indicates the end of the input.

Output

For each case, your program must print the matrix corresponding to the next moment using the same format than the one of the input. It must print a line feed after each matrix.

Author

Salvador Roura (en: Carlos Molina)

© Jutge.org, 2006–2025.

Public test cases
  • Input

    2 3
    X.X
    .X.
    2 2
    XX
    XX
    0 0
    

    Output

    .X.
    .X.
    
    XX
    XX
    
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++