第1章-生命游戏&幻方(2020.03.01)

第1章-生命游戏&幻方


(此时本人并没有步入神奇的C++世界)

1. 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.

For example:
【输入】
5 3
5 4
5 5
5 6
-1 -1 //输入结束
3 //输出第3代的结果
【输出】
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>

const int maxrow = 20, maxcol = 60;
void print(int data[][maxcol+2]);
void update(int data[][maxcol+2], int n);

int main()
{
    int array[maxrow+2][maxcol+2];
    int x,y;
    for(x = 0; x<=maxrow+1; x++){
        for(y=0; y<=maxcol+1; y++){
            array[x][y] = 0;
        }
    }
    int i,j;
    scanf("%d%d",&i,&j);
    while(i != -1 && j!= -1){
        array[i][j] = 1;
        scanf("%d%d",&i,&j);
    }
    int n;
    scanf("%d",&n);
    update(array, n);
    print(array);
    return 0;
}

void print(int data[][maxcol+2])
{
    int i,j;
    for(i=1; i<=maxrow; i++){
        for(j=1; j<=maxcol; j++){
            if(data[i][j] == 0)
                printf("-");
            else printf("*");
        }
        printf("\n");
    }
}

void update(int data[][maxcol+2], int n)
{
    int i,j,x,y,z;
    int data2[maxrow+2][maxcol+2];
    for(z=1; z<=n; z++){
        for(i=0; i<=maxrow+1; i++){
            for(j=0; j<=maxcol+1; j++){
                data2[i][j] = data[i][j];
            }
        }
        for(i=1; i<=maxrow; i++){
            for(j=1; j<=maxcol; j++){
                int sum = 0;
                for(x=i-1; x<=i+1; x++){
                    for(y=j-1; y<=j+1; y++){
                        sum += data2[x][y];
                    }
                }
                sum -= data2[i][j];
                if(data[i][j]==1){
                    if(sum==2 || sum==3)
                        data[i][j] = 1;
                    else
                        data[i][j] = 0;
                }
                else if(data[i][j]==0){
                    if(sum==3){
                        data[i][j] = 1;
                    }
                }
            }
        }
    }
}
2. Magic Square【中等】

A magic square is a square array or integers such that the sum of every row, the sum of every column, and sum of each of the two diagonals are all equal.
Write a program that generates a magic square by the following method. This method works only when the size of the square is an odd number. Start by placing 1 in the middle of the top row. Write down successive integers 2, 3, … along a diagonal going upward and to the right. When you reach the top row(as you do immediately since 1 is in the top now), continue to the bottom row as though the bottom row were immediately above the top row. When you reach the rightmost column, continue to the leftmost column as though it were immediately to the right of the rightmost one. When you reach a position that is already occupied, instead drop straight down one position from the previous number to insert the new one. The 55 magic square constructed by this method is shown as follows.
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
.
【输入】奇数n,1<=n<=101.
【输出】n
n的magic square.(数字之间为两个空格,每行最后一个数字后面无空格。)

For example,
【输入】 5
【输出】
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,i,j,x,y;
    scanf("%d",&n);
    int array[n][n];
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            array[i][j] = 0;
        }
    }
    i=0;
    j=(n-1)/2;
    for(m=1; m<=n*n; m++){
        array[i][j] = m;
        x = i-1;
        y = j+1;
        if(x<0 && y>0 && y<n){//上出框
            x = n-1;
        }
        else if(x<0 && y==n){//斜出框
            x = n-1;
            y = 0;
        }
        else if(x>=0 && x<n && y==n){//右出框
            y = 0;
        }
        //判断位置是否被占
        if(array[x][y] != 0){
            x = i+1;
            y = j;
        }
        i = x;
        j = y;
    }
    for(i=0; i<n; i++){
        for(j=0; j<n-1; j++){
            printf("%d  ",array[i][j]);
        }
        printf("%d",array[i][j]);
        printf("\n");
    }
    return 0;
}
3. One-Dimensional Life【中等】

One-Dimensional Life takes place on a straight line instead of a rectangular grid. Each cell has four neighboring positions: those at distance one or two from it on each side. The rules are similar to those of two-dimensional Life except (1) a dead cell with either two or three living neighbors will become alive in the next generation, and (2) a living cell dies if it has zero, one, or three living neighbors. (Hence a dead cell with zero, one, or four living neighbors stays dead; a living cell with two or four living neighbors stays alive.) The progress of sample communities is shown in Figure 1.6(Textbook page44). Design, write, and test a program for one-dimensional Life.
The total count of the cells is less than 60.

Input: the position of living cells (1<=postion<=60. Terminate the list with the special number -1).
The number (n) of generation. (n=0 means the initial Grid)
Output: the next n generations of the grid.

For example:
【输入】
5 7 -1
1
【输出】
-----*------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

const int maxrow = 60;
void print(int data[]);
void update(int data[], int n);

int main()
{
    int array[maxrow+5];
    int i;
    for(i=0; i<maxrow+5; i++){
        array[i] = 0;
    }
    scanf("%d",&i);
    while(i != -1){
        array[i+1] = 1;
        scanf("%d",&i);
    }
    int n;
    scanf("%d",&n);
    update(array, n);
    print(array);
    return 0;
}

void print(int data[])
{
    int i;
    for(i=2; i<=maxrow+1; i++){
        if(data[i] == 0)
           printf("-");
        else printf("*");
    }
}

void update(int data[], int n)
{
    int i,j,k,sum;
    int data2[maxrow+5];
    for(k=1; k<=n; k++){
        for(i=0; i<=maxrow+5; i++){
            data2[i] = data[i];
        }
        for(i=2; i<=maxrow+1; i++){
            sum = 0;
            for(j=i-2; j<=i+2; j++){
                sum += data2[j];
            }
            sum -= data2[i];
            if(data[i]==1){
                if(sum==2 || sum==4)
                   data[i] = 1;
                else
                   data[i] = 0;
                }
            else if(data[i]==0){
                 if(sum==3 || sum==2)
                    data[i] = 1;
            }
         }
     }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值