和Poj1753一样,但是Poj2965要求输出路径,暴力就完事儿了
http://poj.org/problem?id=1753
http://poj.org/problem?id=2965
//Poj2965
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int maxnum = 17;
int a[4][4];
int ans = maxnum;
vector<int> m, n,p,q;
int judge() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (a[i][j] != 0) {
return 0;
}
}
}
return 1;
}
void flip(int row, int col)
{
a[row][col] = !a[row][col];
for (int i = 0; i < 4; i++)
{
if (i != col)
a[row][i] = !a[row][i];
if (i != row)
a[i][col] = !a[i][col];
//a[col][i] = !a[row][i];
}
}
int dfs(int x, int y, int t) {
if (judge()) {
if (t < ans)
{
ans = t;
p = m;
q = n;
}
return 0;
}
if (x >= 4 || y >= 4)return 0;
int nx = (x + 1) % 4;
int ny = y + (x + 1) / 4;
//int ny = (y + 1) % 4;
//int nx = x + (y + 1) / 4;
dfs(nx, ny, t);
flip(x, y);
m.push_back(x);
n.push_back(y);
dfs(nx, ny, t + 1);
flip(x, y);
m.pop_back();
n.pop_back();
return 0;
}
int main() {
char temp[5];
for (int i = 0; i < 4; i++) {
gets_s(temp);
for (int j = 0; j < 4; j++) {
a[i][j] = (temp[j] == '+' ? 1 : 0);
}
}
dfs(0, 0, 0);
if (ans < maxnum) {
cout << ans << endl;
for (int i = 0; i < p.size(); i++)
{
cout << p[i]+1 << " " << q[i]+1 << endl;
}
}
else printf("Impossible\n");
}