| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2150 人关注过本帖
标题:关于一维life game的代码,请看一下错在什么地方
只看楼主 加入收藏
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
结帖率:96.88%
收藏
已结贴  问题点数:20 回复次数:10 
关于一维life game的代码,请看一下错在什么地方
#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编辑过]

搜索更多相关主题的帖子: int num grid for return 
2020-03-09 14:37
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
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
【输出】
-----*------------------------------------------------------

我想要两颗西柚。
2020-03-09 14:38
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
//这是二维的规则,因为上面一维的规则要参照二维
//老师给了我们二维的标答,但是我仿照二维数组写这串一维数组的代码的时候始终无法输出正确结果,看了好几遍也看不出问题
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代的结果
【输出】
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
---**-------------------------------------------------------
--*--*------------------------------------------------------
---**-------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------

我想要两颗西柚。
2020-03-09 14:39
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
//害,发现了一个点,就是构造的数组超出范围了,修改一下,但还是不对
#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编辑过]


我想要两颗西柚。
2020-03-09 15:28
纯蓝之刃
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:76
帖 子:570
专家分:3727
注 册:2019-7-29
收藏
得分:0 
没读明白题,例程是个怎么个繁衍方式没推算出来结果

一沙一世界,一花一天堂。无限掌中置,刹那成永恒。
2020-03-09 15:55
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


我的理解就是在某一代中一个活着的点有2或4个活着的邻居,它在下一代就继续活着;死了的点有2或3个活着的邻居,它就复活;
其他情况下下一代的点都是死的

我想要两颗西柚。
2020-03-09 17:08
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
生命游戏呀,经典题目

2020-03-09 19:00
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
这道题好像不是一种死亡情况

2020-03-09 19:07
纯蓝之刃
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:76
帖 子:570
专家分:3727
注 册:2019-7-29
收藏
得分:0 
这个和自动扫雷规则有些像,主要的不是在生和死的变换上,而是在计算矩阵的时候,整个矩阵要在四周留下一圈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编辑过]


一沙一世界,一花一天堂。无限掌中置,刹那成永恒。
2020-03-09 22:13
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
回复 9楼 纯蓝之刃
我想问的是一维的情况QAQ

我想要两颗西柚。
2020-03-09 22:16
快速回复:关于一维life game的代码,请看一下错在什么地方
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053730 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved