题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<cstring>
using namespace std;
char g[6][6];
char backup[6][6];
int dx[5] = {0, 0, 1, 0, -1}, dy[5] = {0, 1, 0, -1, 0};
void turn(int x, int y)
{
for (int i = 0; i < 5; i ++)
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 5 && b >= 0 && b < 5)
g[a][b] ^= 1;
}
}
int work()
{
int ans = 1e7;
for (int i = 0; i < 1 << 5; i ++)//枚举第一行的操作;
{
memcpy(backup, g, sizeof g);
int res = 0;
for (int j = 0; j < 5; j ++)
if (i >> j & 1)
{
turn(0, j);
res ++;
}
for (int j = 0; j < 4; j ++)
for (int k = 0; k < 5; k ++)
if (g[j][k] == '0')
{
turn(j + 1, k);
res ++;
}
bool successful = true;
for (int j = 0; j < 5; j ++)
if (g[4][j] == '0')
{
successful = false;
break;
}
if (successful) ans = min(res, ans);
memcpy(g, backup, sizeof backup);
}
if (ans > 6) return -1;
return ans;
}
int main()
{
int T;
cin >> T;
while(T --)
{
for (int i = 0; i < 5; i ++)
for (int j = 0; j < 5; j ++)
cin >> g[i][j];
cout << work() << endl;
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla