翻转问题 Fliptile POJ - 3279

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
//#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:10240000,10240000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<ctime>
#include<ctype.h>
#include<stdlib.h>
#include<bitset>
#include<algorithm>
#include<assert.h>
#include<numeric> //accumulate
#define endl "\n"
#define fi first
#define se second
#define forn(i,s,t) for(int i=(s);i<(t);++i)
#define mem(a,b) memset(a,b,sizeof(a))
#define rush() int MYTESTNUM;cin>>MYTESTNUM;while(MYTESTNUM--)
#define debug(x) printf("%d\n",x)
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pf(x) printf("%d\n",x)
#define pf2(x,y) printf("%d %d\n",x,y)
#define pf3(x,y,z) printf("%d %d %d\n",x,y,z)
#define ll long long
#define ull unsigned long long
#define dd double
#define pfs puts("*****")
#define pfk(x) printf("%d ",(x))
#define kpf(x) printf(" %d",(x))
#define pfdd(x) printf("%.5f\n",(x));
#define pfhh printf("\n")
using namespace std;
const ll P=1e9;
ll mul(ll a, ll b){ll ans = 0;for(;b;a=a*2%P,b>>=1) if(b&1) ans=(ans+a)%P;return ans;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
const int maxn=100;
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch==‘-‘;ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int ma[maxn][maxn];
int aft[maxn][maxn];
int ans[maxn][maxn];
int dir[4][2]={{0,1}, {1,0}, {-1,0}, {0,-1}};
signed main()
{
    int n,m;
    sc2(n,m);
    for(int i=0;i<n;++i)
        for(int j=0;j<m;++j) sc(ma[i][j]);

    bool flag=0;
    for(int i=0; i<1<<m; ++i){

        for(int j=m-1;j>=0;--j) ans[0][j] = i>>j&1;//左右相反

        for(int k=0;k<n;++k)
            for(int j=0;j<m;++j)
                aft[k][j]=ma[k][j];

        for(int k=m-1; k>=0; --k)
            if(i>>k&1){
                aft[0][k] ^= 1;
                for(int w=0; w<4; ++w){
                    int xx=0+dir[w][0], yy=k+dir[w][1];
                    if(xx>=0 && yy>=0 && xx<n && yy<m) aft[xx][yy] ^= 1;
                }
            }

        for(int j=0; j<n-1; ++j)
            for(int k=0; k<m; ++k)
                if(aft[j][k]==1){
                    ans[j+1][k]=1;
                //reverse begin
                    aft[j+1][k] ^= 1;
                    for(int w=0; w<4; ++w){
                        int xx=j+1+dir[w][0], yy=k+dir[w][1];
                        if(xx>=0 && yy>=0 && xx<n && yy<m) aft[xx][yy] ^= 1;
                    }
                //reverse end
                }else ans[j+1][k]=0;

        bool f=1;
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
                if(aft[i][j]==1){
                    f=0;break;
                }
        if(f){
            for(int i=0;i<n;++i){
                for(int j=0;j<m;++j) printf("%d ",ans[i][j]);
                puts("");
            }
            flag=1;
            break;
        }
    }
    if(!flag) puts("IMPOSSIBLE");

    return 0;
}

原文地址:https://www.cnblogs.com/LS-Joze/p/11565498.html

时间: 2024-10-11 17:01:08

翻转问题 Fliptile POJ - 3279的相关文章

Enum:Fliptile(POJ 3279)

Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一样,一个翻了,另一个就要跟着一起翻,第一行会影响下面所有行,而且影响情况只有一种,所以枚举完了以后我们不断模拟就可以了 1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 5 usin

Fliptile(POJ 3279)

原题如下: Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16494   Accepted: 6025 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 i

Fliptile POJ 3279(反转)

原题 题目链接 题目分析 对于N行M列的矩阵,先看看左上角的点,能使该点翻转的有(1,1).(1,2)和(2,1),无法确定该通过哪个点来翻转(1,1).不妨先确定好第一行的翻转情况,也就是说该不该翻(1,1)和(1,2)已经确定了,则此时只有(2,1)能翻转点(1,1),这时候就能直接判断了,因此可以确定第二行的翻转情况,同理迭代到最后一行,则整个矩阵的翻转情况都已确认完毕,此时只需要检验最后一行是否有黑色-1的即可.这里枚举第一行的翻转情况可用二进制枚举,则总的复杂度为O(N*M*2M).

D - Fliptile POJ - 3279

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

Fliptile POJ - 3279(状态压缩)

题意:给你一个n * m的矩阵,元素为0/1, 求把所有的元素变成0所需要的最少操作.(每对一个格子操作,该十字格的元素反转) 分析:一看数据量 <= 15, 就有点状态压缩的感jio.从小到大枚举第一行的操作,因为第一行的操作决定了后面所有的操作,所以最后判断对于第一行的操作是不是符合题意即可. 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <

状态压缩+枚举 POJ 3279 Fliptile

题目传送门 1 /* 2 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 3 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 20; 11 const int INF = 0x3f3f3f3

POJ 3279(Fliptile)题解

以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑色所需翻转的是哪些棋子(这句话好长...). [题目分析] 盯着奇怪的题目看了半天发现和奶牛没什么卵关系..奶牛智商高了产奶高是什么鬼说法... 这题刚开始被放到搜索的分类下了..然而这和搜索有什么关系..没经验所以想了各种和搜索沾边的方法,结果没想出解法,直到看了网上的题解,压根不是搜索啊有木有.

POJ 3279 Fliptile(翻转)

题目链接:http://poj.org/problem?id=3279 题意:n*m棋盘区域,由0和1填充,0白,1,黑,(正反两面颜色各异)通过翻转操作使棋盘全部白色朝上,每次翻转棋子,棋子周围(上下左右)都要被翻转,问是否可能翻转 翻转成功,如果翻转成功,输出最少的翻转次数下每个棋子的翻转次数. 题解:有m列,所以最多有2^m种可能性,枚举第一行.然后从第i行(i从2开始)先开始考虑第i-1行,然后依次往下推,再判断最后一行是不是都翻转成白色. 1 #include <string> 2

poj 3279 Fliptile (简单搜索)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 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 whic