题目如下:
输入样例:
-+--
----
----
-+--
输出样例:
6
1 1
1 3
1 4
4 1
4 3
4 4
思路 or 题解:
数据量很小可以直接进行 搜索
在这里我使用 二进制枚举 的方法去寻找答案
时间复杂度:
O
(
2
n
)
O(2^n)
O(2n)
我们二进制枚举可以通过 除 与 模 来确定位置
AC 代码如下:
/*
Make it simple and keep self stupid
author:Joanh_Lan
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <bitset>
#include <set>
#include <random>
#include <ctime>
#include <queue>
#include <stack>
#include <climits>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
// #define int long long
#define ll long long
#define PII pair<int, int>
#define px first
#define py second
typedef std::mt19937 Random_mt19937;
Random_mt19937 rnd(time(0));
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2147483647;
const int N = 100009;
// int Mod(int a,int mod){return (a%mod+mod)%mod;}
// int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
// int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
// int inv(int a,int mod){return qmi(a,mod-2,mod);}
// int lcm(int a,int b){return a*b/__gcd(a,b);}
int s[5][5];
int f[5][5];
vector<PII> tt;
void copy()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
f[i][j] = s[i][j];
}
void solve()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
char ch;
cin >> ch;
if (ch == '-')
s[i][j] = 1;
else
s[i][j] = 0;
}
auto func = [](int x, int y)
{
tt.push_back({x, y});
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i == x || j == y)
f[i][j] ^= 1;
};
auto check = []()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (!f[i][j])
return false;
return true;
};
int mn = inf;
vector<PII> ans;
for (int i = 0; i < (1 << 16); i++)
{
// i = 53261;
int cnt = 0;
tt.clear();
copy();
for (int j = 0; j < 16; j++)
{
if (i & (1 << j))
cnt++, func(j / 4, j % 4);
}
if (cnt < mn && check())
{
mn = cnt;
ans.clear();
ans = tt;
}
}
cout << mn << '\n';
for (auto [x, y] : ans)
cout << x + 1 << ' ' << y + 1 << '\n';
}
int main()
{
buff;
int _ = 1;
// cin >> _;
while (_--)
solve();
}