1. 程式人生 > >二進制搜索

二進制搜索

cati nes 1.0 chang ace 運算 hit can lan

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

推薦博客 :http://www.cnblogs.com/caitian/p/5396946.html

題意 : 翻牌子 , 每次可以把想要翻改的位置和其上下左右四個方向都變換一下,問是否存在滿足要求的翻法 , 使最後的板子全部是 0 朝上,輸出翻轉次數最小的翻法, 1 表示翻。

思路 : 每翻動一塊板子,都會影響到他的四個方向,因此可以這樣,枚舉出第一行的所有翻法,會有一些板子0向上,一些1向上,現在固定第一行的翻法,那麽第一行的那些1向上的板子只能通過第二行去改變 ,
  其核心就是一句話 , 當前行是否翻轉取決於上一行 。
  還有,對於第一行的每個板子 , 只有翻或不翻 , 枚舉下所有的情況,0 ~ 1<<m ,在利用位運算把每一位都提取出來。

代碼 :
  
/*
 * Author:  ry 
 * Created Time:  2017/10/26 17:35:49
 * File Name: 2.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long
int n, m;
int pre[20][20]; // 存儲最初的情況
int mid[20][20]; // 存儲中間情況    
int ans[20][20]; // 存儲最終結果
int dir[5][2] = {1, 0, -1, 0, 0, 1, 0, -1, 0, 0}; // 每次檢驗的 5 個方向

bool get(int x, int y){
    int cnt = pre[x][y];
    
    for(int i = 0; i < 5; i++){
        int fx = x + dir[i][0];
        int fy = y + dir[i][1];
        if (fx >= 0 && fx < n && fy >= 0 && fy < m)
            cnt += mid[fx][fy];
    }    
    return cnt & 1;
}

int cal(){
   
    for(int i = 1; i < n; i++){
        for(int j = 0; j < m; j++){
            if (get(i-1, j)) mid[i][j] = 1;
        }
    }
    for(int i = 0; i < m; i++){
        if (get(n-1, i)) return inf;
    } 
    int cnt = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cnt += mid[i][j];
        }
    }
    return cnt;
}

int main() {

    while(~scanf("%d%d", &n, &m)){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                scanf("%d", &pre[i][j]);
            }
        }        
        
        int cnt = inf;
        for(int i = 0; i < 1<<m; i++){ // 按照字典序枚舉所有的情況
            memset(mid, 0, sizeof(mid));
            for(int j = 0; j < m; j++){
                mid[0][j] = i>>j&1;
            }
            int temp = cal();
            if(temp < cnt){
                cnt = temp;
                memcpy(ans, mid, sizeof(ans));
            } 
        }
        if (cnt == inf) printf("IMPOSSIBLE\n");
        else {
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    printf("%d", ans[i][j]); 
                    if (j == m-1) printf("\n");
                    else printf(" ");
                }
            }
        }
    }
    return 0;
}


二進制搜索