每日一题06_飞行员兄弟

Posted on 2020-07-30 15:01  Nicela  阅读(125)  评论(0)    收藏  举报

#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 5;

char g[N][N], backup[N][N];

int get(int x, int y)
{
return x * 4 + y;
}

void turn_one(int x, int y)
{
if (g[x][y] == '+') g[x][y] = '-';
else g[x][y] = '+';
}

void turn_all(int x, int y)
{
for (int i = 0; i < 4; i ++ )
{
turn_one(x, i);
turn_one(i, y);
}

turn_one(x, y);
}

int main()
{
for (int i = 0; i < 4; i ++ ) cin >> g[i];

vector<PII> res;
for (int op = 0; op < 1 << 16; op ++ )
{
vector<PII> temp;
memcpy(backup, g, sizeof g); // 备份

// 进行操作
for (int i = 0; i < 4; i ++ )
for (int j = 0; j < 4; j ++ )
if (op >> get(i, j) & 1)
{
temp.push_back({i, j});
turn_all(i, j);
}

// 判断所有灯泡是否全亮
bool ison = true;
for (int i = 0; i < 4; i ++ )
for (int j = 0; j < 4; j ++ )
if (g[i][j] == '+')
ison = false;

if (ison)
{
if (res.empty() || res.size() > temp.size()) res = temp;
}

memcpy(g, backup, sizeof g); // 还原
}

cout << res.size() << endl;
for (auto op : res) cout << op.x + 1 << ' ' << op.y + 1 << endl;

return 0;
}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3