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