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?




class Solution {
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size(), n = m ? board[0].size() : 0;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				int count = 0;
				for (int I = max(i - 1, 0); I<min(i + 2, m); ++I)
					for (int J = max(j - 1, 0); J < min(j + 2, n); ++J)
						count += board[I][J] & 1;
				if (count == 3 || count - board[i][j] == 3)
					board[i][j] |= 2;

		for (int i = 0; i < m;i++)
			for (int j = 0; j < n; j++)
				board[i][j] >>= 1;


Life Game


Problem DescriptionnRich people are playing life game,generally it is like they find some poor people. And they brainwash those people,make them believe that they have to kill all other to live. And they watch the procedure and bet on the results for fun. Of course they will kill the winner when the winner believe that he wins.nToday's game is different from before.There is a n*m matrix of people. Now you are the organizer and you can control those people's life.nIf people at row i, column j, survive at the end, you will be rewarded with wi,j money.Otherwise you are rewarded with bi,j money.nAlso,those rich people have some strange request, each one will point out a contiguous sub matrix of this matrix and say: if people in this sub matrix all die(or survive), you arenrewarded with s money.nYou don't really care about those people's life, so you want to maximize the money you get.nWhat is the maximum money you can get if you kill those people optimally?nPlease note that there's no restriction on killing people, you can kill no one, you can also kill everyone.n(Following text is irrelevant with the problem)nIn the end, you kill everybody, and you find that you have been brainwashed too, and those people you killed are actually your friends and family. Then you get crazy and kill yourself, and those rich people enjoy a lot.nAnd the true organizer, I, get all the money supposed to be yours :).n nnInputnThe first line contains an integer T(1<=T<=5).Denoting the number of the test cases.nFor each test cases,the first line contains three integers n, m, r(n<=50, m<=50, r<=50000),denote the number of rows,the number of columns and the number requests.nThen follows a n * m integer matrix b.nThen follows a n * m integer matrix w.n0<=bi,j , wi,j<=100nThen r lines follows,each represent a request.nEach lines contains 6 integers r1, c1, r2, c2, t, s. It means the sub-matrix's top left corner is (r1, c1), and the bottom right corner is (r2, c2). t = 0 if they should survive, t = 1 if they should die. s is the reward you can get if this request are fulfilled. 0<=s<=10000.n nnOutputnFor each test cases,print the answer in one line.n nnSample Inputn2n2 2 3n34 44n63 30n1 9n53 57n1 2 2 2 1 2843n1 1 2 1 0 2169n2 1 2 1 1 6980n2 2 3n50 93n65 70n52 28n91 25n1 1 2 1 0 9862n2 1 2 1 1 1876n2 2 2 2 0 4190n nnSample Outputn9994n14313 问答