AcWing 116. 飞行员兄弟
原题链接
简单
作者:
RwChen
,
2021-06-23 11:15:46
,
所有人可见
,
阅读 183
1s内能跑完
C++ 代码
#include <iostream>
#include <cstring>
using namespace std;
char g[4];
char temp[4];
char ops[4];
bool GetBit(char c, int b)
{
return c & (1 << b);
}
void SetBit(char &c, int b, bool f)
{
if(f)
c |= (1 << b);
else
c &= ~(1 << b);
}
void FlipBit(char &c, int b)
{
c ^= (1 << b);
}
void Change(int x, int y)
{
temp[x] ^= 15;
for(int j = 0; j < 4; j++)
{
if(j == x)
continue;
FlipBit(temp[j], y);
}
}
void OutPut_xy()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(GetBit(ops[i], j))
cout << i+1 << ' ' << j+1 << endl;
}
}
}
//暴力枚举2^16次方种情况
int Solution()
{
int ans = 17;
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16; j++)
{
for(int k = 0; k < 16; k++)
{
for(int l = 0; l < 16; l++)
{
memcpy(temp, g, sizeof g);
int op = 0;
for(int b = 0; b < 4; b++)
{
if(GetBit(i, b))
{
Change(0, b);
op++;
}
if(GetBit(j, b))
{
Change(1, b);
op++;
}
if(GetBit(k, b))
{
Change(2, b);
op++;
}
if(GetBit(l, b))
{
Change(3, b);
op++;
}
if(op > ans)
break;
}
if(op > ans)
break;
int sum = temp[0] + temp[1] + temp[2] + temp[3];
if(!sum)
{
if(op < ans)
{
ans = op;
ops[0] = i;
ops[1] = j;
ops[2] = k;
ops[3] = l;
}
}
}
}
}
}
return ans;
}
int main()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
char c;
cin >> c;
if(c == '+')
SetBit(g[i], j, 1);
else
SetBit(g[i], j, 0);
}
}
cout << Solution() << endl;
OutPut_xy();
return 0;
}