这道题目比较简单,只是对于题目的理解可能不太好理解。题目中说total density cannot ,however , exceed 3 or 0 ,其实就是说如果大于3就等于3处理,小于0就等于0来处理。下面是代码(方法比较简陋):
#include <iostream>
#include <stdio.h>
#include <fstream>
using std::ifstream ;
using namespace std ;
int day = 0 ;
int a[16] = {0} ;
int square[21][21] = {0} ;
int sum[21][21] = {0} ;
void getSum();
void refreshSqr() ;
int main()
{
cin >> day ;
for(int i = 0 ; i < 16 ; ++i)
{
cin >> a[i] ;
}
for(int j = 0 ; j < 20 ;++j)
{
for(int k = 0 ; k < 20 ;++k)
{
cin >> square[j][k] ;
}
}
for(int m = 0 ; m < day ; ++m)
{
getSum();
refreshSqr() ;
}
for(int l = 0 ; l < 20 ;++l)
{
for(int n = 0 ; n < 20 ;++n)
{
if(square[l][n]==0)
{
cout << ".";
}
else if(square[l][n] == 1)
{
cout << "!" ;
}
else if(square[l][n] == 2)
{
cout << "X" ;
}
else
cout << "#" ;
}
cout << endl ;
}
return 0 ;
}
void getSum()
{
for(int i = 0 ; i < 20 ; ++i)
{
for(int j = 0 ; j<20 ; ++j)
{
if(i==0 && j==0)
{
sum[i][j] = square[i][j]+square[i+1][j]+square[i][j+1] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(i==0 && j==19)
{
sum[i][j] = square[i][j]+square[i][j-1]+square[i+1][j] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(i==19 && j==0)
{
sum[i][j] = square[i][j]+square[i][j+1]+square[i-1][j] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(i==19 && j==19)
{
sum[i][j] = square[i][j] + square[i][j-1] + square[i-1][j] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(i==0)
{
sum[i][j] = square[i][j] + square[i][j-1] + square[i][j+1] + square[i+1][j] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(i==19)
{
sum[i][j] = square[i][j] + square[i-1][j]+square[i][j+1]+square[i][j-1] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(j==0)
{
sum[i][j] = square[i][j] + square[i-1][j]+square[i+1][j]+square[i][j+1] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else if(j==19)
{
sum[i][j] = square[i][j] + square[i-1][j]+square[i+1][j]+square[i][j-1] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
else
{
sum[i][j] = square[i][j] + square[i-1][j]+square[i+1][j]+square[i][j+1]+square[i][j-1] ;
if(sum[i][j] > 15 || sum[i][j] < 0)
{
exit(1) ;
}
}
}
}
}
void refreshSqr()
{
for(int m = 0 ; m<20 ; ++m)
{
for(int n = 0 ; n < 20 ; ++n)
{
square[m][n] = square[m][n] + a[sum[m][n]] ;
if(square[m][n] > 3)
{
square[m][n] = 3 ;
}
else if(square[m][n] < 0)
{
square[m][n] = 0 ;
}
}
}
}