• 二进制搜索


    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
    ");
            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("
    ");
                        else printf(" ");
                    }
                }
            }
        }
        return 0;
    }
    

    东北日出西边雨 道是无情却有情
  • 相关阅读:
    55.UIbutton点击切换颜色
    54.NSJSONSerialization类进行json解析(字符串“UTF-8解码”)
    53.设置内容的行间距
    第二阶段冲刺7
    第二阶段冲刺6
    第十四周
    第二阶段冲刺5
    第二阶段冲刺4
    第二阶段冲刺3
    第二阶段冲刺2
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7738220.html
Copyright © 2020-2023  润新知