2017年3月18日 | ljfcnyali
题目大意
有44的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使44的正方形变为纯白或者纯黑?
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
题目分析
进行暴力搜索,枚举每一个答案是否可行。
AC代码
/*************************************************************************
> File Name: POJ1753.cpp
> Author: ljf-cnyali
> Mail: ljfcnyali@gmail.com
> Created Time: 2017/3/18 11:15:07
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)
const int maxn = 10000;
int n, m, step;
int Map[6][6];
int dt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool flag = false;
void flip(int x, int y) {
Map[x][y] = 1 - Map[x][y];
REP(i, 0, 3) {
int u = x + dt[i][0];
int v = y + dt[i][1];
Map[u][v] = 1 - Map[u][v];
}
}
bool pd() {
REP(i, 1, 4)
REP(j, 1, 4)
if(Map[i][j] != Map[1][1])
return false;
return true;
}
void dfs(int x, int y, int now) {
if(step == now) {
flag = pd();
return ;
}
if(flag || x == 5)
return ;
flip(x, y);
if(y < 4)
dfs(x, y + 1, now + 1);
else
dfs(x + 1, 1, now + 1);
flip(x, y);
if(y < 4)
dfs(x, y + 1, now);
else
dfs(x + 1, 1, now);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
char c;
REP(i, 1, 4) {
REP(j, 1, 4) {
c = getchar();
if(c == 'b')
Map[i][j] = 1;
}
getchar();
}
for(step = 0; step <= 16; ++ step) {
dfs(1, 1, 0);
if(flag) {
break;
}
}
if(flag)
printf("%d\n", step);
else
printf("Impossible\n");
return 0;
}