Leetcode Game of Life

Leetcode Game of Life 相关代码,本题逻辑上相对简单,就是简单的迭代。但是题目要求使用in place完成,这样就得思考一下了,我的实现方案是,先行对数组进行扫描,每个位置对应的数字的高位记录邻居为“1”的个数,最低位记录当前的状态,这样可以保证得到了新的信息又不损失以前的信息,在所有位置扫描完后,只需根据邻居为“1”的个数和当前状态,直接更新下一个状态即可,相关代码以及测试如下:

#include<iostream>
#include<vector>
#include<climits>
#include<cstdlib>

using namespace std;
// In this program, we need to implement in-place, so we used the high order
// bit to record the "1" surround the board[i][j], and the lowest bit record
// the current value. After calculated these value, we update the corespond
// value.
class Solution {
public:
    void gameOfLife(vector<vector<int> >& board) {
        if (board.size() == 0) {
            return;
        }
        int m = board.size();
        int n = board[0].size();
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                setValue(i, j, m, n, board);
            }
        }
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                evaluate(i, j, board);
            }
        }
    }

    // Calculate the number of "1" in board[i][j] 8 neighbor's.
    // For record these information in-place, we use the char
    // high order bit to recorde the number of "1", use the
    // lowest bit to record the origin value
    void setValue(int i, int j, int m, int n, vector<vector<int> >& board) {
        int count = 0;
        for (int row = i - 1; row <= i + 1; row ++) {
            for (int col = j - 1; col <= j + 1; col ++) {
                if ((row >= 0 && row < m) && (col >= 0 && col < n)) {
                    if ((row != i || col != j) && (board[row][col] & 1) == 1) {
                        count ++;
                    }
                }
            }
        }
        count <<= 1;
        board[i][j] = count + board[i][j];
    }

    // Decide the state of the next iteration for the pre information
    // which recorded in the high order bits.
    void evaluate(int i, int j, vector<vector<int> >& board) {
        int current_state = board[i][j] & 1;
        int count = board[i][j] >> 1;
        if (current_state == 0 && count == 3) {
            board[i][j] = 1;
        } else if (current_state == 1) {
            if (count < 2) {
                board[i][j] = 0;
            } else if (count > 3) {
                board[i][j] = 0;
            } else {
                board[i][j] = current_state;
            }
        } else {
            board[i][j] = current_state;
        }
    }
};

void print_cur(vector<vector<int> >& board) {
    for (int i = 0; i < board.size(); i ++) {
        for (int j = 0; j < board[0].size(); j ++) {
            cout<<board[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}

int main(int argc, char* argv[]) {
    Solution so;
    int row = atoi(argv[1]);
    int col = atoi(argv[2]);
    vector<vector<int> > test(row, vector<int>(col, 1));
    for (int i = 0; i < row; i ++) {
        for (int j = 0; j < col; j ++) {
            test[i][j] = rand() % 2;
        }
    }
    print_cur(test);
    for (int time = 0; time < (row + col) * 2; time ++) {
        cout<<"time: "<<time<<endl;
        so.gameOfLife(test);
        print_cur(test);
    }
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值