poj 3279 Fliptile(dfs+二进制枚举)

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

题意:有一个n*m的棋盘,每个格子都有黑白两面(0表示白色,1表示黑色)。我们需要把所有的格子都反转成白色,注意每翻转一个格子,它上下左右的格子都会跟着反转。问在最少翻转的情况下,每个格子反转的次数。

题解:利用第一层的 2^n 种状态来进行枚举。此时能够反转第一行的只有第二行,所以我们可以通过第一行的情况依次向下翻转,直到判断到最后一行。最后判断最后一行是否全为白色来确定当前方案是否可行。我们枚举第一行所有的情况,共2^m种可能。 时间复杂度为O(n*m*2^m)。(如果不利用第一层的状态枚举,我们就需要枚举每个格子,,,时间复杂度(2^m*n))

由于情况只有翻与不翻两种,所以我们可以利用二进制枚举
ps:其实每个格子最多只能翻一次,,,多了也没用不是吗~~~所以只需要顺着遍历就可以了,不用担心后面翻了,前面是否需要回溯的问题~

不懂二进制枚举的戳下方~
https://blog.csdn.net/ling_wang/article/details/81353592

#include
#include
#include
#include
using namespace std;

int N, M;
const int inf = 0x3f3f3f3f;
int mp[20][20];
int step[20][20];//记录最后结果
int d[5][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0, 0}};//范围为5的区域都要改变
int vis[20][20];//标记是否踩过

int check(int x, int y){//查询(x,y)的颜色,该颜色由它最初的状态及周围四个棋子是否已翻转决定
    int now = mp[x][y];
    for(int i = 0; i < 5; i++){
        int x1 = x + d[i][0];
        int y1 = y + d[i][1];
        if(x1 >= 0 && x1 < N && y1 >= 0 && y1 < M)
            now += vis[x1][y1];
    }
    return now%2;//如果为0说明该棋子为白,不需要翻转,如果为1说明需要翻转
}

int dfs(){
    for(int i = 1; i < N; i++){
        for(int j = 0; j < M; j++){
            if(check(i-1, j)){//上方格子是黑色,则必须必须反转(i,j)号格子,因为上方格子如果不翻转,再没机会翻转了
                vis[i][j] = 1;
                //cout << vis[i][j] << endl;
            }
        }
    }
    for(int i = 0; i < M; i++){//判断最后一行是否全为白
        if(check(N-1, i))
            return -1;
    }
    int s = 0;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            s += vis[i][j];//统计当前方案翻转总次数
            //cout << s << endl;
        }
    }
    return s;
}

int main(){
    while(scanf("%d%d", &N, &M) != EOF){
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++){
                scanf("%d", &mp[i][j]);
            }
        }
        int ans = inf;
        bool flag = 0;
        for(int i = 0; i < (1 << M); i++){//第一行有 1<
            memset(vis, 0, sizeof(vis));
            for(int j = 0; j < M; j++){
                vis[0][M-j-1] = i >> j & 1; //利用二进制枚举第一行所有的情况
            }
            int sum = dfs();
            if(sum < ans && sum >= 0){
                ans = sum;
                //cout << ans << endl;
                memcpy(step, vis, sizeof(vis));//复制数组
                flag = 1;
            }
        }
        if(!flag)
                printf("IMPOSSIBLE\n");
        else{
            for(int i = 0; i < N; i++){
                for(int j = 0; j < M; j++){
                    if(j != M-1)
                        printf("%d ", step[i][j]);
                    else
                        printf("%d\n", step[i][j]);
                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(dfs,基本算法,枚举)