zoukankan      html  css  js  c++  java
  • (简单) POJ 3279 Fliptile,集合枚举。

      Description

      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".

      题目大意就是说一个棋盘的黑白棋,反转一个周围的也会反转,然后问都变成0的最小次数的时候,每一个分别反转多少次。

      首先应该想到说每一个最多反转1次,因为反转2次和0次是等价的,所以说每一个就是0或1了。

      然后就是说第一行确定了的话第二行也会确定下来,因为第一行某一个的上左右都确定下来了,所以下也会确定,所以可以通过第一行推出之后的所有。

      然后就是第一行的问题了。

      N和M都小于等于15,2^N不会超时。直接枚举第一行就行。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    int ans[20][20];
    int map1[20][20];
    int lasans[20][20];
    int M,N;
    int minans;
    
    int getdata()
    {
        for(int i=1;i<=M;++i)
            for(int j=1;j<=N;++j)
                cin>>map1[i][j];
    }
    
    inline void slove()
    {
        minans=1e8;
        int L=(1<<N);
        int rem;
        bool ok;
    
        for(int i=0;i<L;++i)
        {
            rem=0;
    
            for(int j=1;j<=N;++j)
                if((1<<(j-1))&i)
                {
                    ans[1][j]=1;
                    ++rem;
                }
                else
                    ans[1][j]=0;
    
            for(int j=2;j<=M;++j)
            {
                for(int k=1;k<=N;++k)
                {
                    if((map1[j-1][k]+ans[j-1][k]+ans[j-2][k]+ans[j-1][k-1]+ans[j-1][k+1])%2)
                    {
                        ans[j][k]=1;
                        ++rem;
                        if(rem>minans)
                            goto next1;
                    }
                    else
                        ans[j][k]=0;
                }
            }
    
            ok=1;
            for(int j=1;j<=N;++j)
                if((map1[M][j]+ans[M][j]+ans[M-1][j]+ans[M][j-1]+ans[M][j+1])%2)
                {
                    ok=0;
                    break;
                }
    
            if(ok)
                if(rem<minans)
                {
                    minans=rem;
                    for(int i=1;i<=M;++i)
                        for(int j=1;j<=N;++j)
                            lasans[i][j]=ans[i][j];
                }
    
            next1:;
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        while(cin>>M>>N)
        {
            memset(ans,0,sizeof(ans));
            memset(map1,0,sizeof(map1));
    
            getdata();
            slove();
    
            if(minans==1e8)
                cout<<"IMPOSSIBLE
    ";
            else
                for(int i=1;i<=M;++i,cout<<endl)
                    for(int j=1;j<=N;++j)
                        cout<<lasans[i][j]<<' ';
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    在Linux终端命令行下播放音乐的命令
    危险,几条可致命的Linux命令!
    Linux 之 shell 比较运算符
    Linux-dd命令详解
    vi总结的几个技巧
    用iptables实现代理上网
    CentOS7.5 开启Samba服务
    CentOS7.5常用命令
    CentOS7.5 安装部署Apache+Mysql+Php
    C++入门基础笔记
  • 原文地址:https://www.cnblogs.com/whywhy/p/4228141.html
Copyright © 2011-2022 走看看