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

Follow up:

  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

提示:

由于题目中要求所有点的状态需要一次性发生改变,而且不用额外的空间,这是本题的最大难点。

既然需要“就地解决”,我们不妨分析一下borad的特性:board上的元素有两种状态,生(1)和死(0)。这两种状态存在了一个int型里面。所以我们可以有效利用除最低位的其它位,去保存更新后的状态,这样就不需要有额外的空间了。

具体而言,我们可以用最低位表示当前状态,次低位表示更新后状态:

  • 00(0):表示当前是死,更新后是死;
  • 01(1):表示当前是生,更新后是死;
  • 10(2):表示当前是死,更新后是生;
  • 11(3):表示当前是神,更新后是生。

代码:

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int height = board.size();
        int width = height ? board[0].size() : 0;
        if (!height || !width) {
            return;
        }
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                int life = getlives(board, height - 1, width - 1, i, j);
                if (board[i][j] == 1 && (life == 2 || life == 3)) {
                    board[i][j] = 3;
                } else if (board[i][j] == 0 && life == 3) {
                    board[i][j] = 2;
                }
            }
        }
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                board[i][j] >>= 1;
            }
        }
    }

    int getlives(vector<vector<int>>& board, int height, int width, int i, int j) {
        int res = 0;
        for (int h = max(i-1, 0); h <= min(i+1, height); ++h) {
            for (int w = max(j-1, 0); w <= min(j+1, width); ++w) {
                res += board[h][w] & 1;
            }
        }
        res -= board[i][j] & 1;
        return res;
    }
};
时间: 2024-10-13 01:41:27

289. Game of Life的相关文章

NYOJ 289 苹果

苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 ctest有n个苹果,要将它放入容量为v的背包.给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值. 输入 有多组测试数据,每组测试数据第一行为2个正整数,分别代表苹果的个数n和背包的容量v,n.v同时为0时结束测试,此时不输出.接下来的n行,每行2个正整数,用空格隔开,分别代表苹果的大小c和价钱w.所有输入数字的范围大于等于0,小于等于1000. 输出 对每组测试数据输出一个整数,代表能放入背包的苹

nyist 289

#include <stdio.h>int dp[12888];int w[3408],d[3408]; int max(int x,int y) { return x>y?x:y; } int main(){ int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF&&(n||m)) { for(i=0;i<n;i++) scanf("%d%d",&w[i],&d

[email&#160;protected] [289] Game of Life

https://leetcode.com/problems/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 mathematician John Horton Conway in 1970." Given a board with m by n c

人生的诗&middot;285~289节

285.    夏之寒 深重的汗水浸透衣裳 昭示着夏的炎炎肆虐人间 可心底的寒是天山上的冰雪 刺透心房 挣扎着打滚是一场荒诞的表演 如同这荒诞的生 286.    前行 迈步在深夜 迈步在清霄 唯有前路的方向不曾改变 纵然缓慢 纵然霜冷 也只有前行 前行 在黯黯的黑夜里 287.    年华 生命的华年已消失近半 只留下一地的苍凉狼藉 左眼悲哭着往日的碌碌 右眼欣喜地眺望光阳的彼方 如同梦想与现实的撕裂 熬磨去仅余的激情 只是明日的朝阳又会升起 晚夜的黯黯终作烟尘 288.    书香 湮没在史

Codeforces Round #289 Div2 E

Problem 给一串长度为N的字符串,对于每个字符,若字符为元音,则权值为1,否则为0.一个子串的权值定义为该串所有字符权值之和除以字符个数,一个母串的权值定义为所有子串的权值之和.求母串的权值. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N: [1, 5*10^5] 字符集: 'A'-'Z' 元音: I E A O U Y Solution 考虑每个元音字符对母串的贡献,可以找出规律. More 举"ABCDOEFGHKMN"

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

nyist oj 289 苹果 (动态规划——背包问题)

苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 ctest有n个苹果,要将它放入容量为v的背包.给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值. 输入 有多组测试数据,每组测试数据第一行为2个正整数,分别代表苹果的个数n和背包的容量v,n.v同时为0时结束测试,此时不输出.接下来的n行,每行2个正整数,用空格隔开,分别代表苹果的大小c和价钱w.所有输入数字的范围大于等于0,小于等于1000. 输出 对每组测试数据输出一个整数,代表能放入背包的苹

SCUT - 289 - 小O的数字 - 数位dp

https://scut.online/p/289 一个水到飞起的模板数位dp. #include<bits/stdc++.h> using namespace std; typedef long long ll; bool notp[2000]; const int MAXS1=200; const int MAXS2=2000; int a[20]; ll dp[20][MAXS1][MAXS2]; ll dfs(int pos,int s1,int s2,bool lead,bool l

LeetCode 289 - 生命游戏 - 原地算法

Wikipedia关于原地算法的描述:原地算法(in-place algorithm)基本上不需要额外辅助的数据结构,然而,允许少量额外的辅助变量来转换数据的算法.当算法运行时,输入的数据通常会被要输出的部分覆盖掉. 很容易看出,原地算法的特点是不需要辅助的数据结构而只需要辅助变量.通常,维护一个复杂的数据结构时间空间复杂度都是比较高的,原地算法的优越性正来自于不依赖数据结构,尽管它借鉴了数据结构的思想. 同时也不难看出,原地算法总体的实现思路就是用输出的数据在原地覆盖已经“没用”的输入的数据.

Codeforces Round #289 (Div. 2, ACM ICPC Rules)——B贪心——Painting Pebbles

There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c