Leetcode Game of Life
2015-10-27 00:54
399 查看
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; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- CPP 虚函数、虚函数表及虚拟继承(转)
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解