1. 程式人生 > >康威生命遊戲 Conway's game of life

康威生命遊戲 Conway's game of life

一、需求描述

你的任務是寫一個程式來計算給定起始位置下的下一代康威生命遊戲。

遊戲從一個兩維的網格開始,每一個網格有兩種狀態:存活、死亡。

網格是有限的,沒有生命可以存活在邊界之外。當計算下一代網格時,需要遵循下述四個規則:

1. 任何四周鄰居存活數少於兩個的存活網格將死亡,因為人口稀少。

2. 任何四周鄰居存活數多於三個的存活網格將死亡,因為過度擁擠。

3. 任何四周鄰居存活數等於兩個或三個的存活網格將在下一代中繼續存活。

4. 任何已經死亡的網格,如果周圍鄰居存活數為3個,將重新復活。

二、程式輸入輸出

輸入:當代網格資訊,包括網格大小(即幾行幾列),以及每個網格的狀態(存活或死亡)

輸出:按照上面的規則,計算下一代網格,並輸出

具體的格式參見下面的示例

三、示例

示例: * 存活網格, . 死亡網格

示例輸入:(4 x 8 網格)

4 8

........

....*...

...**...

........

示例輸出:

4 8

........

...**...

...**...

........

四、題目英文原文描述

Your task is to write a program to calculate the nextgeneration of Conway's game of life, given any starting

position. You start with a two dimensional grid of cells,where each cell is either alive or dead. The grid is finite,

and no life can exist off the edges. When calculating thenext generation of the grid, follow these four rules:

1. Any live cell with fewer than two live neighbours dies,   as if caused by underpopulation.

2. Any live cell with more than three live neighbours dies,   as if by overcrowding.

3. Any live cell with two or three live neighbours lives   on to the next generation.

4. Any dead cell with exactly three live neighbours becomes   a live cell.

Examples: * indicates live cell, . indicates dead cell

Example input: (4 x 8 grid)

4 8

........

....*...

...**...

........

Example output:

4 8

........

...**...

...**...

........

C語言程式碼

#include <string.h>
#include <stdio.h>
#include <assert.h>

#define gRow 4
#define gline 8

char gGroupCell[gRow][gline + 1] = {
    "........",
    "....*.*.",
    "...**...",
    "...**..." };

void get_33grid(int row, int line, char CellGrid33[3][3])
{
    int loopnum = 0;
    int row33 = 0;
    int line33 = 0;
    for (row33 = 0; row33 < 3; row33++)
    {
        for (line33 = 0; line33 < 3; line33++)
        {
            if (((0 == row) && (0 == row33)) ||
                (((gRow - 1) == row) && (2 == row33)) ||
                ((0 == line) && (0 == line33)) ||
                (((gline - 1) == line) && (2 == line33)))
            {
                CellGrid33[row33][line33] = '.';
                continue;
            }
            CellGrid33[row33][line33] = gGroupCell[row - 1 + row33][line - 1 + line33];
        }
    }
}

char middle_cell_handle(char InputCell[3][3])
{
    int row = 0;
    int line = 0;
    int neighbor = 0;
    //char CharCell[3][3];
    //int  IntCell[3][3];
    char MiddleCell = '.';

    //memset(CharCell, 0, sizeof(CharCell));
   // memset(IntCell, 0, sizeof(IntCell));

    if (NULL == InputCell)
        return '.';

    //memcpy(IntCell, InputCell, sizeof(InputCell));

    for (row = 0; row < 3; row++)
    {
        for (line = 0; line < 3; line++)
        {
            if ('*' == InputCell[row][line])
                neighbor += 1;
        }
    }
    if (3 == neighbor)
    {
        MiddleCell = '*';
    }
    else if (('*' == InputCell[1][1]) && (2 == neighbor))
    {
        MiddleCell = '*';
    }
    else
    {
        MiddleCell = '.';
    }
    return MiddleCell;
}

char *cell_life(char InputCell[4][9])
{
    int row = 0;
    int line = 0;
    char ChildCell[gRow][gline + 1] = { 0 };
    char CellGrid33[3][3] = { 0 };

    for (row = 0; row < gRow; row++)
    {
        for (line = 0; line < gline; line++)
        {
            get_33grid(row, line, CellGrid33);
            ChildCell[row][line] = middle_cell_handle(CellGrid33);
        }
        ChildCell[row][gline] = '\0';
    }
    memcpy(gGroupCell, ChildCell, sizeof(gGroupCell));
    return gGroupCell;
}

void print_cell_group(char InputCell[gRow][gline + 1])
{
    int row = 0;
    int line = 0;
    for (row = 0; row < gRow; row++)
    {
        for (line = 0; line < gline; line++)
        {
            printf("%c", InputCell[row][line]);
        }
        printf("\n");
    }
    printf("\n============\n");
}


char test_cell_life(int row, int line)
{
    char CellGrid33[3][3] = { 0 };
    get_33grid(row, line, CellGrid33);
    return middle_cell_handle(CellGrid33);
}

test_case()
{
    assert('*'==test_cell_life(1, 3));
    assert('*' == test_cell_life(1, 4));
    assert('*' == test_cell_life(1, 5));
    assert('.' == test_cell_life(1, 6));
    assert('.' == test_cell_life(2, 4));
}

void main()
{
#if 0
    print_cell_group(gGroupCell);
    cell_life(gGroupCell);
    print_cell_group(gGroupCell);
    system("pause");
#else
    test_case();
    system("pause");
#endif

}



相關推薦

生命遊戲 Conway's game of life

一、需求描述你的任務是寫一個程式來計算給定起始位置下的下一代康威生命遊戲。遊戲從一個兩維的網格開始,每一個網格有兩種狀態:存活、死亡。網格是有限的,沒有生命可以存活在邊界之外。當計算下一代網格時,需要遵循下述四個規則:1. 任何四周鄰居存活數少於兩個的存活網格將死亡,因為人

pygame生命遊戲模擬_python Game of life生命棋畫素級別模擬作者:李興球

"""Conway's Game of Life),又稱康威生命棋,是英國數學家約翰·何頓·康威在1970年發明的細胞自動機。 本程式用pygame模擬這種現象,作者:李興球,風火輪少兒程式設計 www.scratch8.net 以前早就聽說過Game of Life,以為

生命遊戲 第一部分-基本功能實現(C++ & Windows SDK)

生命遊戲簡介 生命遊戲其實是一個零玩家遊戲,它包括一個二維矩形世界,這個世界中的每個方格居住著一個活著的或死了的細胞。一個細胞在下一個時刻生死取決於相鄰八個方格中活著的或死了的細胞的數量。如果相鄰方格活著的細胞數量過多,這個細胞會因為資源匱乏而在下一個時

生命遊戲的簡單實現

生命遊戲,數學家John Conway發明的一個遊戲,又稱康威生命演化,生命棋,細胞自動機。 康威有許多好玩有趣的發明,最廣為人知的一個是外觀數列(Look-and-Say),這裡不多說,另一個就是生命遊戲(Game-of-Life)。 關於康威,摘錄一段Wikipedia

用 React + es6 完成一個著名的生命遊戲Game of lifeConway

http://elevenbeans.github.io/2017/04/05/Game-of-life/ 這是有名的康威生命遊戲, 描述的是一種細胞自動機。 對一個 M*N 的區域,每一個位置有兩種狀態,1為活細胞,0為死細胞,對於每個位置都滿足如下的條件: 如

演算法 | 生命遊戲 (Game of Life)

演算法 | 生命遊戲 概要: 本文將要介紹一個名為“生命遊戲”的有趣演算法並用C++進行簡單的實現。 關鍵字: C++;生命遊戲演算法   1 背景說明    生命遊戲,又叫康威生命遊戲(Conway’s Game of Life),或康威生命棋,是英國數

[Swift]LeetCode289. 生命遊戲 | Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathemati

生命遊戲(Game of Life)

大二了,想進一下學校的技術團隊,團隊的一輪的面試過了,二輪面試就是技術考核了,然後就發了一個讓我這菜鳥“目瞪狗呆”的考核試題,題目就是我這篇部落格的標題,“生命遊戲(Game of Life)” ,剛看到這個題目的時候整個人都懵(men)逼了,在腦子裡就閃

視 java B/S 開發 流程

最近專案用到了一個呼叫海康威視攝像頭實現外網網頁預覽的需求,由於第一次接觸這類需求,不免有些焦頭爛額,現終於將需求實現,故,作一筆記用於記憶,並希望幫到要用的人。廢話不多說,直接開始: 注:本文中所有的使用的程式碼和工具以及安裝包的下載地址如下:       傳送門:

leetcode289- Game of Life- medium

not put ray ble || target his present for According to the Wikipedia‘s article: "The Game of Life, also known simply as Life, is a cellul

【leetcode】289. Game of Life

count sel 結果 for span append ins 直接 instead 題目如下: 解題思路:因為本題要求的是直接在輸入的數組上面修改,而且細胞的生死轉換是一瞬間的事情,所以需要引入兩個中間狀態,待死和待活。這兩個中間狀態用於在四條規則判斷的時候是“活”和

289. Game of Life

prev 一位 con and 技術 pan without 移動 之前 一、題目   1、審題        2、分析     編程實現 “LIFE” 這個遊戲:       ①、若 m 的周圍8個鄰居細胞中存活的細胞數 < 2,則此細胞從存活狀態變為死亡狀態;

python leetcode 289. Game of Life

class Solution(object): def gameOfLife(self, board): """ :type board: List[List[int]] :rtype: void Do not return anyth

LeetCode Game of Life 解題

1. 非原地的解答,通過四周補0可以很自然的按照規則寫出下一代的狀態 Game of Life According to the Wikipedia's article: "The Game of Life, also known simply as Life, is

LeetCode 289. Game of Life (C++)

題目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British math

Python 實現 Game of life

團隊內部組織現場程式設計活動,實現這個Game of life。現場沒寫出來,思路是有的。 結尾討論時領導提到了陣列,一下想到了用Numpy. 網址:https://bitstorm.org/gameoflife/ 程式碼: Python2.7 # -*- coding: ut

[leetcode 289]Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British math

HDU - 5973 Game of Taking Stones (佐夫博弈 高精度)

-a span side 模板 multi amount 公式 native str 題目描述: Two people face two piles of stones and make a game. They take turns to take stones. As

HDU - 5973 Game of Taking Stones 佐夫博弈+高精度

威佐夫博弈的模板題 判斷(√5-1)/2 *(b-a)是否和a相等 但是資料很大,用Java開了高精度,二分求√5的值 import java.util.*; import java.math.*; public class Main { public static void

Conway生命遊戲

圖像 接口 per 有一個 c語言 當前 大於 戰爭 有著   版權申明:本文為博主窗戶(Colin Cai)原創,歡迎轉帖。如要轉貼,必須註明原文網址   http://www.cnblogs.com/Colin-Cai/p/9986679.html