注册 登录
编程论坛 C语言论坛

关于一维life game的代码,请看一下错在什么地方

komorebi0110 发布于 2020-03-09 14:37, 2382 次点击
#include<stdio.h>
int judge(int a[],int x)
  {   int num=0;
      for(int i=x-2;i=x+2;i++)
      if((i!=x)&&a[i]) num++;
      if(num==2)return 1;
      if((a[x])&&num==4) return 1;
      if(a[x]==0&&num==3)return 1;
      return 0;
  }
  void update(int a[])
  {   int b[62];
      for(int j=0;j<62;j++)
      b[j]=a[j];
      for(int j=1;j<61;j++)
      a[j]=judge(b,j);
  }
void print(int a[])
{
      for(int j=1;j<61;j++){
       if(a[j]) printf("*");
        else printf("-");}
}
int main()
  {   int grid[62];
      for(int i=0;i<62;i++)
      grid[i]=0;
      int x;
      scanf("%d",&x);
      while(x!=-1){
          grid[x]=1;
          scanf("%d",&x);}
      int n;
      scanf("%d",&n);
      for(int i=0;i<n;i++)
          update(grid);
      print(grid);
      return 0;
  }

[此贴子已经被作者于2020-3-9 16:20编辑过]

10 回复
#2
komorebi01102020-03-09 14:38
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
【输出】
-----*------------------------------------------------------
#3
komorebi01102020-03-09 14:39
//这是二维的规则,因为上面一维的规则要参照二维
//老师给了我们二维的标答,但是我仿照二维数组写这串一维数组的代码的时候始终无法输出正确结果,看了好几遍也看不出问题
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代的结果
【输出】
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
---**-------------------------------------------------------
--*--*------------------------------------------------------
---**-------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
#4
komorebi01102020-03-09 15:28
//害,发现了一个点,就是构造的数组超出范围了,修改一下,但还是不对
#include<stdio.h>
#include<string.h>
int judge(int a[],int x)
  {   int num=0;
      for(int i=x-2;i=x+2;i++)
      if((i!=x)&&a[i]) num++;
      if(num==2)return 1;
      if((a[x])&&num==4) return 1;
      if(a[x]==0&&num==3)return 1;
      return 0;
  }
  void update(int a[])
  {   int b[64];
      for(int j=0;j<64;j++)
      b[j]=a[j];
      for(int j=2;j<62;j++)
      a[j]=judge(b,j);
  }
void print(int a[])
{
      for(int j=2;j<62;j++){
       if(a[j]) printf("*");
        else printf("-");}
}
int main()
  {   int grid[64];
      for(int i=0;i<64;i++)
      grid[i]=0;
      int x;
      scanf("%d",&x);
      while(x!=-1){
          grid[x+1]=1;
          scanf("%d",&x);}
      int n;
      scanf("%d",&n);
      for(int i=0;i<n;i++)
          update(grid);
      print(grid);
      return 0;
  }


[此贴子已经被作者于2020-3-9 15:46编辑过]

#5
纯蓝之刃2020-03-09 15:55
没读明白题,例程是个怎么个繁衍方式没推算出来结果
#6
komorebi01102020-03-09 17:08
只有本站会员才能查看附件,请 登录


我的理解就是在某一代中一个活着的点有2或4个活着的邻居,它在下一代就继续活着;死了的点有2或3个活着的邻居,它就复活;
其他情况下下一代的点都是死的
#7
return_02020-03-09 19:00
生命游戏呀,经典题目
#8
return_02020-03-09 19:07
这道题好像不是一种死亡情况
#9
纯蓝之刃2020-03-09 22:13
这个和自动扫雷规则有些像,主要的不是在生和死的变换上,而是在计算矩阵的时候,整个矩阵要在四周留下一圈0,这样在进行统计的时候才会把各点的数据统计进行一般化处理,且不用考虑边界问题。不用按照正常的角、边、腹地分类考虑。

程序代码:
#include <stdio.h>

int array[22][62]= {0}; //点号状态
int sum[22][62]= {0};   //点号周围八个点状态
int num=0;

void array_around(int x,int y);
void array_generation(int x,int y);       //进行下一代
void array_printf(void);

int main()
{
    int i,j,k;

    while(1)
    {
        scanf("%d %d",&i,&j);
        if(i==-1&&j==-1)
            break;
        else
            array[i][j]=1;
    }
    scanf("%d",&num);

    array_printf();
    for(k=0; k<num; k++)
    {
        for(i=1; i<=20; i++)
            for(j=1; j<=60; j++)
                array_around(i,j);       //统计数据

        for(i=1; i<=20; i++)
            for(j=1; j<=60; j++)
                array_generation(i,j);       //进行下一代

        array_printf();
    }

    return 0;
}

void array_around(int x,int y)
{
    int a[9]= {0,x-1,x-1,x+1,x+1,x-1,x+1,x,x};
    int b[9]= {0,y,y+1,y,y+1,y-1,y-1,y-1,y+1};
    int i,j;

    sum[x][y]=0;
    for(i=1; i<=8; i++)
    {
        if(array[a[i]][b[i]])
            sum[x][y]++;
    }
}

void array_generation(int x,int y)
{
    if(array[x][y])
    {
        switch(sum[x][y])
        {
        case 2:
        case 3:
            array[x][y]=1;
            break;
        default:
            array[x][y]=0;
        }
    }
    else
    {
        if(sum[x][y]==3)
            array[x][y]=1;
    }
}

void array_printf()     //打印
{
    int i,j;

    for(i=1; i<=20; i++)
    {
        for(j=1; j<=60; j++)
        {
            if(array[i][j])
                printf("*");
            else
                printf("-");
        }
        printf("\n");
    }
    printf("\n\n");
}


[此贴子已经被作者于2020-3-9 22:17编辑过]

#10
komorebi01102020-03-09 22:16
回复 9楼 纯蓝之刃
我想问的是一维的情况QAQ
#11
纯蓝之刃2020-03-09 22:40
一维的情况,你给的条件不对,我在程序里标注了,你想好后在相应的地方改了就行了。

程序代码:
#include <stdio.h>

int array[22]= {0}; //点号状态
int sum[22]= {0};   //点号周围八个点状态
int num=0;

void array_around_left(int x,int y);
void array_around_right(int x,int y);
void array_generation(int x);       //进行下一代
void array_printf(void);

int main()
{
    int i,k;

    while(1)
    {
        scanf("%d",&i);
        if(i==-1)
            break;
        else
            array[i]=1;
    }
    scanf("%d",&num);

    array_printf();
    for(k=0; k<num; k++)
    {
        for(i=1; i<=20; i++)
        {
            sum[i]=0;
            array_around_left(i,i);       //统计数据
            array_around_right(i,i);       //统计数据

            printf("%d ",sum[i]);    //调试打印,后期删除
        }
        printf("\n");                //调试打印,后期删除

        for(i=1; i<=20; i++)
            array_generation(i);       //进行下一代

        array_printf();
    }

    return 0;
}

void array_around_left(int x,int y)
{
    if(array[x-1])
    {
        sum[y]++;
        array_around_left(x-1,y);
    }
}

void array_around_right(int x,int y)
{
    if(array[x+1])
    {
        sum[y]++;
        array_around_right(x+1,y);
    }
}

void array_generation(int x)
{
    if(array[x])
    {
        if(sum[x]==2||sum[x]==4)    //活着的邻居数
            array[x]=1;
        else
            array[x]=0;
    }
    else
    {
        if(sum[x]==3||sum[x]==3)    //死的邻居数
            array[x]=1;
        else
            array[x]=0;
    }
}

void array_printf()     //打印
{
    int i;

    for(i=1; i<=20; i++)
    {
        if(array[i])
            printf("*");
        else
            printf("-");
    }
    printf("\n\n");
}
1