小技巧是只用修改状态发生变化的位置。
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
int cur[105][105],nxt[105][105];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int n,m;
int main()
{
cin >> n;
memset(cur, 0, sizeof(cur));
memset(nxt, 0, sizeof(nxt));
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
char tmp;
cin >> tmp;
switch(tmp)
{
case '.':
nxt[i][j] = 1;
break;
case '#':
nxt[i][j] = 0;
break;
case '@':
nxt[i][j] = -1;
break;
default:
break;
}
}
cin >> m;
for( int k = 1; k <= m-1; k++ )
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
cur[i][j] = nxt[i][j];
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
if( cur[i][j] == -1 )
{
for( int d = 0; d < 4; d++ )
{
if( cur[i+dx[d]][j+dy[d]] != 0 )
nxt[i+dx[d]][j+dy[d]] = -1;
}
}
}
}
int ans = 0;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
if( nxt[i][j] == -1 )
ans++;
cout << ans << endl;
return 0;
}