即生命游戏,今天下午迅雷的笔试编程题。
简单的版本,给定当前的状态,给出下一个时刻的状态。
status函数获取某点当时周围的活的细胞数量,
代码写的比较渣
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
#define MAX 10010
int status(vector<vector<char> > v, int m, int n, int row, int col)
{
int live = 0;
for (int i = m - 1; i <= m + 1; ++i)
{
for (int j = n - 1; j <= n + 1; ++j)
{
if (!(i == m && j == n) && i < row && i >= 0 && j >= 0 && j< col)
{
if (v[i][j] == '*')
{
live++;
}
}
}
}
return live;
}
int main(int argc, char const *argv[])
{
int row = 0;
int col = 0;
cin >> row >> col;
vector<vector<char> > v(row);
vector<vector<char> > result(row);
for (int i = 0; i < row; ++i)
{
v[i].resize(col);
}
for (int i = 0; i < row; ++i)
{
result[i].resize(col);
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cin >> v[i][j];
result[i][j] = v[i][j];
}
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (v[i][j] == '.')
{
if(status(v, i, j, row, col) == 3)
result[i][j] = '*';
else
result[i][j] = '.';
}
else
{
if (status(v, i, j, row, col) < 2)
result[i][j] = '.';
else if (status(v, i, j, row, col) > 3)
result[i][j] = '.';
else
result[i][j] = '*';
}
}
}
// for (int i = 0; i < row; ++i)
// {
// for (int j = 0; j < col; ++j)
// {
// cout << status(v, i, j, row, col);
// }
// cout << endl;
// }
// cout << status(v, 0, 0, row, col) << endl;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cout << result[i][j];
}
cout << endl;
}
return 0;
}