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

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


#include
#include
#include
#include
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=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>map1[i][j];
        }
    }
}

int main(){
    while(cin>>M>>N){
        
        init();
        enumerate();
        if(key == -1){
            cout<<"IMPOSSIBLE"<

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


#include
#include
#include
#include
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<=0){
        flipBit(x,j-1);
    }
}

bool test(int sf){
    int rowf = sf;
    nowsteps=0;
    memcpy(map2,map1,sizeof(map2)); 
    //按枚举翻转
    for(int i = 0 ;i>value;
            setBit(map1[i],j,value);
        }
    }
}

int main(){
    while(cin>>M>>N){
        
        init();
        enumerate();
        if(key == -1){
            cout<<"IMPOSSIBLE"<

你可能感兴趣的:(kuangbin带你飞]专题一 简单搜索 D)