zoukankan      html  css  js  c++  java
  • D

    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

    核心思想:把第一排的所有翻法都枚举一遍
    然后依次往后判断怎么翻
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <xfunctional>
    using namespace std;
    int dir[5][2] = { {0,1} ,{0,-1}, {1,0}, {-1,0} ,{0,0} };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    #define ll  long long;
    #define PII  pair<int, int>;
    const int mod = 1e9 + 7;
    const int maxn = 16;
    //if(x<0 || x>=r || y<0 || y>=c)
    int m, n;
    int tile[maxn][maxn], flip[maxn][maxn],res[maxn][maxn];
    
    int get(int x, int y)//判断颜色
    {
        int state = tile[x][y];
        for (int i = 0; i < 5; i++)
        {
            int xn = x + dir[i][0], yn = y + dir[i][1];
            if (0 <= xn && xn < m && 0 <= yn && yn < n)
            {
                state += flip[xn][yn];
            }
        }
        return state % 2;
    }
    int cal()
    {
        for (int i = 1; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (get(i - 1, j) != 0)//如果上一排的颜色不是白色,则需要翻
                {
                    flip[i][j] = 1;
                }
            }
        }
        for (int j = 0; j < n; j++)//判断最后一排是否全是白色
        {
            if (get(m - 1, j) != 0)
                return -1;
        }
        int res = 0;
        for (int i = 0; i < m; i++)//数一下一共翻了多少次
        {
            for (int j = 0; j < n; j++)
            {
                res += flip[i][j];
            }
        }
        return res;
    }
    int main()
    {
        cin >> m >> n;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> tile[i][j];
            }
        }
    
        int ans = -1;
        for (int i = 0; i < 1 << n; i++)//第一排的所有情况
        {
            memset(flip, 0, sizeof(flip));
            for (int j = 0; j < n; j++)//把二进制数转到数组里面
            {
                flip[0][n - j - 1] = i >> j & 1;
            }
            int num = cal();//记录翻转次数
            if (num >= 0 && (ans<0 || ans>num))//如果翻转的次数更小,更新结果数组
            {
                ans = num;
                memcpy(res, flip, sizeof(flip));
            }
        }    
        if(ans<0)
            printf("IMPOSSIBLE
    ");
        else
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    printf("%d%c", res[i][j], j + 1 == n ? '
    ' : ' ');
                }
            }
        }
        return 0;
    }
     
  • 相关阅读:
    鼠标移上与移出事件
    最小高度 最大高度
    鼠标移上改变光标
    设置圆角
    iframe 子窗口获取父窗口元素 父窗口获取子窗口元素
    parent
    禁用滚动条
    mysql--user表
    mysql客户端连不上数据库
    linux下安装MySQL
  • 原文地址:https://www.cnblogs.com/dealer/p/12551704.html
Copyright © 2011-2022 走看看