标签:des style http color io os ar strong for
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 31140 | Accepted: 13551 |
Description
Consider the following position as an example: Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
#include<stdio.h>
#include<algorithm>
using namespace std;
const int row = 4, col = 4;
typedef unsigned int unit;
const unit allup = 0x0000ffff, alldown = 0;
int res = row * col;
void turn(int i, int j, unit& x) {
if (i >= 0 && i < row && j >= 0 && j < col) {
int idx = i*col+j;
x ^= (1 << idx);
}
}
void flip(int i, int j, unit& x) {
turn(i,j,x);
turn(i+1,j,x);
turn(i-1,j,x);
turn(i,j+1,x);
turn(i,j-1,x);
}
void dfs(int s, int cnt, unit& x) {
if (x == allup || x == alldown) {
res = min(res, cnt);
return;
}
if (s >= row*col)
return;
dfs(s+1, cnt, x);
int i = s / row, j = s % row;
flip(i,j,x);
dfs(s+1, cnt+1, x);
flip(i,j,x);
}
int main() {
int i, j, idx;
freopen("E:\\test.txt","r",stdin);
unit init = 0, allup = 0x00ff, alldown = 0;
char ch;
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) {
scanf("%c",&ch);
idx = i*col+j;
if (ch == 'b')
init |= (1<<idx);
}
scanf("%c",&ch);
}
dfs(0,0,init);
if (res != row*col)
printf("%d\n",res);
else
printf("Impossible\n");
return 0;
}
标签:des style http color io os ar strong for
原文地址:http://blog.csdn.net/taoqick/article/details/39497111