D - Fliptile[POJ - 3279]
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
思路:枚舉所有狀態(tài)矛渴,最大會有2^(15*15),必然會超時(shí)绕德,所以考慮部分狀態(tài)確定整體狀態(tài)宜鸯。只需要枚舉第一行(列)拉盾,之后的行(列)一定要翻轉(zhuǎn)前一列所有黑色棋子赴捞。判斷最后一行是否全白即可座哩。因?yàn)檩敵龆嗔艘粋€(gè)感嘆號卡了很久...
#include<iostream>
#include<memory>
#include<cstring>
#include<cstdio>
using namespace std;
int M,N;
int map1[20][20];
int map2[20][20];
int ans[20][20];
int press=0;
int minsteps=999999999;
int nowsteps=0;
int key = -1;
int mx[4]={1,-1,0,0};
int my[4]={0,0,1,-1};
void flip(int x,int y){
int tempx;
int tempy;
map2[x][y] = !map2[x][y];
//記錄翻轉(zhuǎn)位置
ans[x][y] = 1;
//記錄翻轉(zhuǎn)次數(shù)
nowsteps++;
//四周棋子翻轉(zhuǎn)
for(int i = 0 ;i<4;i++){
tempx = x + mx[i];tempy = y + my[i];
//邊緣判斷
if(tempx<M&&tempy<N&&tempx>=0&&tempy>=0){
map2[tempx][tempy] = !map2[tempx][tempy];
}
}
}
bool test(int f){
nowsteps=0;
memcpy(map2,map1,sizeof(map2));
//按枚舉翻轉(zhuǎn)第一行
for(int j = 0;j<N;j++){
if(f&(1<<N-j-1)){
flip(0,j);
}
}
for(int i = 1 ;i<M;i++){
for(int j=0;j<N;j++){
if(map2[i-1][j]){
flip(i,j);
}
}
}
for(int j = 0 ; j <N;j++){
if(map2[M-1][j]){
return false;
}
}
return true;
}
void enumerate(){
//二進(jìn)制枚舉所有情況
for(int i = 0 ;i<(1<<N);i++){
if(test(i)){
if(nowsteps<minsteps){
minsteps = nowsteps;
key = i;
}
}
}
}
void init(){
minsteps=999999999;key=-1;
memset(map1,0,sizeof(map1));
memset(map2,0,sizeof(map2));
for(int i = 0 ;i<M ;i++){
for(int j = 0;j<N;j++){
cin>>map1[i][j];
}
}
}
int main(){
while(cin>>M>>N){
init();
enumerate();
if(key == -1){
cout<<"IMPOSSIBLE"<<endl;
}else{
memset(ans,0,sizeof(ans));
test(key);
for(int i = 0;i<M;i++){
for(int j =0 ;j<N;j++){
printf("%d%c",ans[i][j],j==N-1?'\n':' ');
}
}
}
}
return 0;
}
這個(gè)問題類似熄燈問題聂使,可以用位運(yùn)算來處理壓縮空間,同時(shí)操作起來更簡單汰规。
#include<iostream>
#include<memory>
#include<cstring>
#include<cstdio>
using namespace std;
int M,N;
int map1[20];
int map2[20];
int ans[20];
int press=0;
int minsteps=999999999;
int nowsteps=0;
int key = -1;
int mx[4]={1,-1,0,0};
int my[4]={0,0,1,-1};
int getBit(int x,int i){
return (x>>i)&1;
}
void setBit(int& x,int i,int value){
if(value){
x|=(1<<i);
}else{
x&=~(1<<i);
}
return;
}
void flipBit(int& x,int i){
x^=(1<<i);
}
void fliprowtile(int& x,int j){
flipBit(x,j);
if(j+1<N){
flipBit(x,j+1);
}
if(j-1>=0){
flipBit(x,j-1);
}
}
bool test(int sf){
int rowf = sf;
nowsteps=0;
memcpy(map2,map1,sizeof(map2));
//按枚舉翻轉(zhuǎn)
for(int i = 0 ;i<M;i++){
//儲存每行的操作
ans[i] = rowf;
for(int j=0;j<N;j++){
if(getBit(rowf,j)){
nowsteps ++;
//翻轉(zhuǎn)當(dāng)前行
fliprowtile(map2[i],j);
}
}
//翻轉(zhuǎn)下一行
if(i+1<M){
map2[i+1] ^= rowf;
}
//下一行的翻轉(zhuǎn)操作要看前一行的燈的狀態(tài)
rowf = map2[i];
}
if(map2[M-1]==0){
return true;
}
return false;
}
void enumerate(){
//二進(jìn)制枚舉所有情況
for(int i = 0 ;i<(1<<N);i++){
if(test(i)){
if(nowsteps<minsteps){
minsteps = nowsteps;
key = i;
}
}
}
}
void init(){
minsteps=999999999;key=-1;
memset(map1,0,sizeof(map1));
memset(map2,0,sizeof(map2));
for(int i = 0 ;i<M ;i++){
for(int j = 0;j<N;j++){
int value;
cin>>value;
setBit(map1[i],j,value);
}
}
}
int main(){
while(cin>>M>>N){
init();
enumerate();
if(key == -1){
cout<<"IMPOSSIBLE"<<endl;
}else{
memset(ans,0,sizeof(ans));
test(key);
for(int i = 0;i<M;i++){
for(int j =0 ;j<N;j++){
printf("%d%c",getBit(ans[i],j),j==N-1?'\n':' ');
}
}
}
}
return 0;
}