Middle-题目34:289. Game of Life

本文介绍了一个经典的细胞自动机游戏——生命游戏的实现方法。该游戏由英国数学家John Horton Conway于1970年提出,通过四个简单规则模拟细胞的生死状态。文章详细解释了如何使用Java编程语言来实现这一游戏,并提供了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目原文:
According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population..
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
题目大意:
给一个m*n的网格,每个网格中有一个细胞,它只有生(1)或死(0)两种状态。每个细胞有8个邻居(水平,垂直,对角线),每经过一个时间周期,按如下规则演化:
1. 若一个活着的细胞周围有少于2个活着的邻居,则它会死于孤独。
2. 若一个活着的细胞周围有2,3个活着的邻居,则它会继续活下去。
3. 若一个活着的细胞周围有3个以上活着的邻居,则它会死于人口过多。
4. 若一个死亡的细胞周围恰有3个活着的细胞,则它会复活,就好像繁殖。

给出一个网格当前的状态,求出下一个时间周期的状态。
题目分析:
直接模拟即可,在数邻居个数的时候要考虑一些边界情况(因为不一定都有8个邻居),数出来邻居之后按规则变化即可。
源码:(language:java)

public class Solution {
    private final int DEAD = 0;
    private final int ALIVE = 1;
    public void gameOfLife(int[][] board) {
        int m=board.length;
        int n=board[0].length;
        int[][] nextState = new int[m][n];
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                //Rule 1:Any live cell with fewer than two live neighbors dies, as if caused by under-population.
                if(board[i][j] == ALIVE && getNeighbours(board,i,j) <= 1)  
                    nextState[i][j] = DEAD;
                //Rule 3:Any live cell with more than three live neighbors dies, as if by over-population.
                else if(board[i][j] == ALIVE && getNeighbours(board,i,j) > 3)  
                    nextState[i][j] = DEAD;
                //Rule 4:Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
                else if(board[i][j] == DEAD && getNeighbours(board,i,j) == 3)
                    nextState[i][j] = ALIVE;
                else
                    nextState[i][j]=board[i][j];
                //System.out.println("i="+i+",j="+j+",neighbours="+getNeighbours(board,i,j,m,n)+",this state="+board[i][j]+",nextState="+nextState[i][j]);
            }
        }
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                board[i][j]=nextState[i][j];
            }
        }
    }
    private int getNeighbours(int[][] board,int i,int j) {
        return getState(board,i-1,j-1) + getState(board,i-1,j) + getState(board,i-1,j+1) + getState(board,i,j-1) + getState(board,i,j+1) + getState(board,i+1,j-1) + getState(board,i+1,j) + getState(board,i+1,j+1);
    }
    private int getState(int[][] board, int i, int j) {
        int m = board.length;
        int n = board[0].length;
        if(i<0 || j<0 || i==m || j==n)
            return 0;
        else 
            return board[i][j];
    }
}

成绩:
1ms,beats 13.28%,众数1ms,86.47%
Cmershen的碎碎念:
Discuss中还有一个神算法,有待研读。

This is the style section, please modify and give me the full code <style> /* Custom Styles for Cart Page */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } /* 调整列宽 - 增加全选列宽度 */ .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; /* 增加宽度 */ text-align: center; vertical-align: middle; white-space: nowrap; /* 防止文字换行 */ } .cart-page-section table.cart td.product-info { width: 37%; /* 微调其他列宽度 */ vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } /* 调整剩余列宽度 */ .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; /* 三列平均分配剩余宽度 */ vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); z-index: 1000; } .cart-footer-actions .button { padding: 10px 20px; margin-left: 10px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; margin-top: 10px; transition: background-color 0.3s; } #partial-checkout:hover { background-color: #0b7dda; } .selected-summary { font-size: 16px; font-weight: bold; } .selected-summary p { margin: 0 0 10px 0; } /* Responsive styles */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-footer-actions { position: sticky; bottom: 0; } .cart-footer-actions > div { flex-direction: column; align-items: flex-start; } .cart-footer-actions .selected-summary { margin-top: 20px; text-align: left; width: 100%; } /* 移动端调整全选列 */ .cart-page-section table.cart td.product-select::before { content: "选择"; } } </style>
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值