kuangbin带你飞]专题一 简单搜索 D

2018-06-13  本文已影响0人  jenye_

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

思路:枚举所有状态,最大会有2^(15*15),必然会超时,所以考虑部分状态确定整体状态。只需要枚举第一行(列),之后的行(列)一定要翻转前一列所有黑色棋子。判断最后一行是否全白即可。因为输出多了一个感叹号卡了很久...


#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];
    
    //记录翻转位置 
    ans[x][y] = 1;
    
    //记录翻转次数 
    nowsteps++;
    
    //四周棋子翻转
    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));
    //按枚举翻转第一行 
    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(){
    //二进制枚举所有情况 
        
        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;
}


这个问题类似熄灯问题,可以用位运算来处理压缩空间,同时操作起来更简单。


#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)); 
    //按枚举翻转
    for(int i = 0 ;i<M;i++){
        //储存每行的操作 
        ans[i] = rowf;
        for(int j=0;j<N;j++){
            if(getBit(rowf,j)){
                nowsteps ++;
                //翻转当前行 
                fliprowtile(map2[i],j);
            }
        }
        //翻转下一行 
        if(i+1<M){
            map2[i+1] ^= rowf;
        }
        //下一行的翻转操作要看前一行的灯的状态 
        rowf = map2[i];
    }
    if(map2[M-1]==0){
        return true; 
    }
    return false;
}

void enumerate(){
    //二进制枚举所有情况     
        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;
}
上一篇 下一篇

猜你喜欢

热点阅读