Life game二维

Definitions:

Life is really a simulation, not a game with players. It takes place on unbounded rectangular grid in which each cell can either be occupied by an organism or not. Occupied cells are called alived; unoccupied cells are called dead. Which cells are alive changes from generation to generation according to the number of neighboring cells that are alive, as follows transition rules:

(1) The neighbors of a given cell are the eight cells that touch it vertically, horizontally, or diagonally.
(2) If a cell is alive but either has no neighboring cells alive or only one alive, then in the next generation the cell dies of loneliness.
(3) If a cell is alive and has four or more neighboring cells also alive, then in the next generation the cell dies of overcrowding.
(4) A living cell with either two or three living neighbors remains alive in the next generation.
(5) If a cell is dead, then in the next generation it will become alive if it has exactly three neighboring cells, no more or fewer, that are already alive. All other dead cells remain dead in the next generation.
(6) All births and deaths take place at exactly the same time.
(7) The size of grid is 20*60

Input:
the coordinates of living cells (Terminate the list with the special pair -1 -1). The number (n) of generation. (n=0 means the initial Grid)

Output:
the next n generations of the grid.

Example

input
5 3
5 4
5 5
5 6
-1 -1 //输入结束
3 //输出第3代的结果
output
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
---**-------------------------------------------------------
--*--*------------------------------------------------------
---**-------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------

My answer

#include <stdio.h>

int neighbor(int a[][62],int x,int y){
    int i;
    int cnt=0;
    for(i=y-1;i<=y+1;i++){
        if(a[x-1][i]==1)
            cnt++;
    }
    for(i=y-1;i<=y+1;i++){
        if(a[x+1][i]==1)
            cnt++;
    }
    if(a[x][y-1]==1)
        cnt++;
    if(a[x][y+1]==1)
        cnt++;
    return cnt;
}

void update(int a[][62]){
    int i,j;
    int b[22][62];
    for(i=0;i<22;i++){
        for(j=0;j<62;j++){
            b[i][j]=a[i][j];
        }
    }//把a复制给b 防止数据不被改变
    
    for(i=1;i<21;i++){
        for(j=1;j<61;j++){
            if(a[i][j]==1){
                if(neighbor(b,i,j)<=1||neighbor(b,i,j)>=4)
                    a[i][j]=0;
            }
            else{
                if(neighbor(b,i,j)==3)
                    a[i][j]=1;
            }
        }
    }
}

void print(int a[][62]){//打印出表格
    int i,j;
     for(i=1;i<21;i++){
        for(j=1;j<61;j++){
            if(a[i][j]==1)
                printf("*");
            else
                printf("-");
        }
        printf("\n");
     }
}

int main()
{
    int a[22][62];//考虑到边沿数据的比较 设置一个空的边框
    int x,y;
    int n,i,j;
    for(i=0;i<22;i++){
        for(j=0;j<62;j++)
            a[i][j]=0;
    }
    while(scanf("%d %d",&x,&y)){
        if(x==-1&&y==-1)
            break;
        else
            a[x][y]=1;
    }
    scanf("%d",&n);
    for(i=0;i<n;i++){
        update(a);
    }
    print(a);
    return 0;
}
发布了13 篇原创文章 · 获赞 0 · 访问量 268

猜你喜欢

转载自blog.csdn.net/Caiyii530/article/details/104662663
今日推荐