题目链接
http://poj.org/problem?id=2965
分析
用二进制表示开关状态,暴力枚举每个位置是否操作即可
AC代码
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = 5;
int h[maxn], cnt;
char s[maxn];
pair<int, int> ans[maxn * maxn];
void dfs(int x, int y) {
if (x == 4) {
for (int i = 0; i < 4; ++i)
if (h[i] != 15) return;
printf("%d\n", cnt);
for (int i = 1; i <= cnt; ++i)
printf("%d %d\n", ans[i].first, ans[i].second);
exit(0);
}
int nx = x, ny = y + 1;
if (ny == 4) ++nx, ny = 0;
dfs(nx, ny);
h[x] ^= 15;
for (int i = 0; i < 4; ++i)
if (i != x) h[i] ^= 1 << y;
++cnt;
ans[cnt].first = x + 1, ans[cnt].second = y + 1;
dfs(nx, ny);
h[x] ^= 15;
for (int i = 0; i < 4; ++i)
if (i != x) h[i] ^= 1 << y;
--cnt;
}
int main() {
for (int i = 0; i < 4; ++i) {
scanf("%s", s);
for (int j = 0; j < 4; ++j)
if (s[j] == '-') h[i] |= 1 << j;
}
dfs(0, 0);
return 0;
}