AcWing 116. 飞行员兄弟
原题链接
简单
作者:
uchar
,
2024-12-06 16:54:47
,
所有人可见
,
阅读 13
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char st1[4][4];
int st[5][5], backup[5][5];
void turn(int x, int y)
{
for (int i = 0; i < 4; i ++ )
st[x][i] ^= 1;
for (int i = 0; i < 4; i ++ )
st[i][y] ^= 1;
st[x][y] ^= 1;
}
int main()
{
int steps = 0, ans = 99, t;
bool success;
for (int i = 0; i <= 3; i ++ )
cin >> st1[i];
for (int i = 0; i < 4; i ++ )
for (int j = 0; j < 4; j ++ )
if (st1[i][j] == '+') st[i][j] = 1;
else st[i][j] = 0;
memcpy(backup, st, sizeof st);
for (int op = 0; op < (1 << 16); op ++ )
{
memcpy(st, backup, sizeof backup);
success = true;
steps = 0;
for (int i = 0; i < 16; i ++ )
{
if (op >> i & 1)
{
steps ++;
turn(i / 4, i % 4);
}
}
for (int i = 0; i < 4; i ++ )
{
for (int j = 0; j < 4; j ++ )
if (st[i][j])
{
success = false;
break;
}
if (!success) break;
}
if (success)
{
if (steps < ans)
{
t = op;
ans = steps;
}
}
}
if (ans < 99)
{
printf("%d\n", ans);
for (int i = 0; i < 16; i ++ )
if (t >> i & 1)
printf("%d %d\n", i / 4 + 1, i % 4 + 1);
}
return 0;
}