kuangbin帶你飛]專題一 簡單搜索 D

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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末汤功,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子溜哮,更是在濱河造成了極大的恐慌滔金,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茂嗓,死亡現(xiàn)場離奇詭異餐茵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)述吸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門忿族,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蝌矛,你說我怎么就攤上這事道批。” “怎么了入撒?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵隆豹,是天一觀的道長。 經(jīng)常有香客問我茅逮,道長璃赡,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任献雅,我火速辦了婚禮碉考,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘挺身。我一直安慰自己侯谁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布瞒渠。 她就那樣靜靜地躺著良蒸,像睡著了一般技扼。 火紅的嫁衣襯著肌膚如雪伍玖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天剿吻,我揣著相機(jī)與錄音窍箍,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛椰棘,可吹牛的內(nèi)容都是我干的纺棺。 我是一名探鬼主播,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼邪狞,長吁一口氣:“原來是場噩夢啊……” “哼祷蝌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起帆卓,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤巨朦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后剑令,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糊啡,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年吁津,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棚蓄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,926評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡碍脏,死狀恐怖梭依,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情典尾,我是刑警寧澤睛挚,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站急黎,受9級特大地震影響扎狱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勃教,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一淤击、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧故源,春花似錦污抬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至门驾,卻和暖如春射赛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奶是。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工楣责, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竣灌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓秆麸,卻偏偏與公主長得像初嘹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子沮趣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容