game of life
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
queue<int> temp_state;
int row = board.size();
int i,j;
if(row < ) return;
int col = board[].size();
if(col <) return;
for(i=;i<row;i++)
for(j=;j<col;j++){
int state_i_j = next_state(board,i,j);
temp_state.push(state_i_j);
if((j == col-)) { //每行最后一个元素 更新
if((i>=)){ //如果不是第一行,更新前一行
for(int z=;z<col;z++){
board[i-][z] = temp_state.front();
temp_state.pop();
}
}
if((i==row-)){ //如果是最后一行,更新当前行
for(int z=;z<col;z++){
board[i][z] = temp_state.front();
temp_state.pop();
}
}
}
}
}
int next_state(vector<vector<int>>& board,int i,int j){
int sum = ;
int row = board.size();
int col = board[].size();
if(board[i][j] == ){
if(i- >=){
if(j->= && board[i-][j-] == ) sum++;
if(board[i-][j] == ) sum++;
if(j+<col && board[i-][j+] == ) sum++;
}
if(i+ < row){
if(j->= && board[i+][j-] == ) sum++;
if(board[i+][j] == ) sum++;
if(j+<col && board[i+][j+] == ) sum++;
}
if(j->= && board[i][j-] == ) sum++;
if(j+< col && board[i][j+] == ) sum++;
if(sum== || sum ==) return ;
return ;
}
if(board[i][j] == ){
if(i- >=){
if(j->= && board[i-][j-] == ) sum++;
if(board[i-][j] == ) sum++;
if(j+<col && board[i-][j+] == ) sum++;
}
if(i+ < row){
if(j->= && board[i+][j-] == ) sum++;
if(board[i+][j] == ) sum++;
if(j+<col && board[i+][j+] == ) sum++;
}
if(j->= && board[i][j-] == ) sum++;
if(j+< col && board[i][j+] == ) sum++;
if(sum ==) return ;
return ;
}
}
};
随机推荐
- jenkins 邮件配置步骤
一.进行系统管理中的邮件配置步骤: 1.设置Extended E-mail Notification 二.对构建的job 添加邮件发送的步骤: 3.成功截图:
- 用java给php写个万能接口
package helloworld; import java.io.IOException; import javax.servlet.ServletException; import javax. ...
- Android --资料集合
google android 官方教程 http://hukai.me/android-training-course-in-chinese/basics/index.html android视频资料 ...
- MFC对话框Dialog控件处理程序handler因为public修饰符导致无法访问
比如说你的Dialog有一个Button名为Confirm,对应IDC_CONFIRM,处理程序handler为OnConfirm 那么OnConfirm必须是protected属性,如果是publi ...
- devexpress13学习系列(三)PDFViewer(3)
PdfDocumentProperties Class 该类,用来显示载入的pdf文件的属性,包括: Name Description Application Indicates the appl ...
- node.js npm权限问题try running this command again as root/Administrator.
npm install报错; try running this command again as root/Administrator. 以管理员身份打开cmd 开始菜单->所有程序->附 ...
- JAVA-面向对象-多态
多态 1.方法重载 2.方法重写 3.对象转型 4.抽象(可以定义类和方法) (关键字 abstract) ( 如: public abstract class robot )(不能修饰 ...
- Aspectj是什么
转载自:http://www.cnblogs.com/sunwke/articles/2568875.html 网上出现了很多讲解 AspectJ 的资料,但大多是从讲解 AspectJ 语法开始,然 ...
- Codeforce Round #218 Div2
A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作数相减就可以了! C:二分题- -,没想到比赛时因为 ...
- [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...