# ZeroJudge - d537: 4. 染色遊戲
### 題目連結:https://zerojudge.tw/ShowProblem?problemid=d537
###### tags: `ZeroJudge` `模擬`
```cpp=
#include <iostream>
#include <cstring>
using namespace std;
int encode[128], dx[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }, dy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
static const auto Initialize = [] {
cin.sync_with_stdio(false); cin.tie(nullptr);
encode['Y'] = 1; encode['B'] = 2; encode['G'] = 3;
encode['R'] = 4; encode['O'] = 5; encode['P'] = 6; encode['D'] = 7;
return nullptr;
}();
int main() {
int ticks = 0, canvasSize, X, Y, lastAmount = 0, targetAmount = 0, board[102][102] = {}, expand[102][102] = {}, newColor, target;
bool changed[102][102];
char color;
cin >> canvasSize;
for (int i = 0; i < 3; ++i) {
cin >> color >> X >> Y, ++X, ++Y;
board[Y][X] = encode[color];
changed[Y][X] = true;
}
cin >> color, target = encode[color];
for (int i = 1; i <= canvasSize; ++i)
for (int j = 1; j <= canvasSize; ++j)
if (board[i][j] == target)
++targetAmount;
while (lastAmount <= targetAmount) {
if (ticks == canvasSize)
break;
lastAmount = targetAmount;
memset(expand, 0, sizeof(expand));
for (int i = 1; i <= canvasSize; ++i)
for (int j = 1; j <= canvasSize; ++j) {
expand[i][j] |= board[i][j];
if (changed[i][j])
for (int k = 0; k < 8; ++k)
expand[i + dy[k]][j + dx[k]] |= board[i][j];
}
memset(changed, false, sizeof(changed));
for (int i = 1; i <= canvasSize; ++i)
for (int j = 1; j <= canvasSize; ++j)
if (board[i][j] != expand[i][j]) {
if (expand[i][j] == target)
++targetAmount;
else if (board[i][j] == target)
--targetAmount;
board[i][j] = expand[i][j], changed[i][j] = true;
}
++ticks;
}
cout << lastAmount << '\n';
}
```