NOI Contest Archive

Archive / 2019 / Selection Test

Game of Life Simulation

Medium · National Olympiad in Informatics Sri Lanka - Screening Test

The game of life is in a grid of N rows and N columns. Each cell in the grid is either dead or alive. In the beginning only a single cell is alive. Then at each steps cells become dead or alive based on the following criteria:

  1. If 1 or 3 of the neighboring cells (up, down, left or right not in diagonal directions) are alive the cell becomes or stays alive.
  2. If 0 or 2 or 4 of the neighboring cells are alive the cell becomes dead or stays dead.

image

The four cells marked in red are the neighboring cells of the black cell. So, if 1 or 3 of the red cells are alive, the black cell will be alive in the next step or dead otherwise.

You are given

  1. N - size of the grid
  2. R and C - the row and column of the first cell alive, (rows are numbered from 1 to N from top to bottom and columns are numbered from 1 to N from left to right)
  3. S - the number of steps

You need to find the number of cells alive after S steps.

Input Format

First row will contain N, the second row will contain R and C separated by a space and the third row will contain S.

Output Format

You should output the total number of cells alive after S steps.

Constraints

Sample Input 0

4
2 2
3

Sample Output 0

6