• 原题如下:

    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 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
  • 题解:首先,同一个格子翻转两次的话就会恢复原状,所以多次反转是多余的,此外,翻转的格子的集合相同的话,其次序是无关紧要的,所以总共有2MN种翻转的方法,由于解空间实在太大,我们必须另寻他径,参考解决POJ3276的方法,那道题中,让最左端的牛反转的方法只有1种,所以只要用直接判断的方法确定就可以了,但在这里,是行不通的,比如左上角的格子,除了翻转(1,1)之外,翻转(1,2)和(2,1)也都可以把(1,1)翻转,所以不能直接套用POJ3276的方法,但是如果假设第一行的翻转方法已经确定,那么翻转(1,1)的就只剩下(2,1)了,所以可以直接判断(2,1)是否需要翻转,类似的第二行都可以判断,如此反复下去就可以判断出所有格子的翻转方法了,判断是否有解,只要看最后一行是不是全为白色即可,如果并非全白,那么就说明不存在可行的操作方法。综上,我们只要先确定出第一行的翻转方式即可,而第一行的翻转方式共有2N种,所以总的时间复杂度为O(MN2N)
  • 代码:
     #include <cstdio>
    #include <cctype>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #define number s-'0' using namespace std; const int MAX_N=;
    const int INF=0x3f3f3f3f;
    const int dx[]={-, , , , };
    const int dy[]={, , , -, };
    int N,M;
    int flip[MAX_N][MAX_N], tile[MAX_N][MAX_N], opt[MAX_N][MAX_N]; void read(int &x){
    char s;
    x=;
    bool flag=;
    while(!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for(x=number;isdigit(s=getchar());x=x*+number);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if(x<)
    {
    putchar('-');
    x=-x;
    }
    if(x>)
    write(x/);
    putchar(x%+'');
    } int calc();
    int get(int, int); int main()
    {
    read(M);read(N);
    for (int i=; i<M; i++)
    for (int j=; j<N; j++)
    read(j[i[tile]]);
    int res=-;
    for (int i=; i< <<N; i++)
    {
    memset(flip, , sizeof(flip));
    for (int j=; j<N; j++)
    {
    flip[][N-j-]=i>>j&;
    }
    int num=calc();
    if (num>= && (res< || num<res))
    {
    res=num;
    memcpy(opt, flip, sizeof(flip));
    }
    }
    if (res<) puts("IMPOSSIBLE\n");
    else
    for (int i=; i<M; i++)
    for (int j=; j<N; j++)
    printf("%d%c", j[i[opt]], j+==N? '\n': ' ');
    } int get(int x, int y)
    {
    int c=tile[x][y];
    for (int i=; i<; i++)
    {
    int x2=x+dx[i], y2=y+dy[i];
    if (<=x2 && x2<M && <=y2 && y2<N)
    {
    c+=y2[x2[flip]];
    }
    }
    return c % ;
    } int calc()
    {
    for (int i=; i<M; i++)
    {
    for (int j=; j<N; j++)
    {
    if (get(i-,j)!=) flip[i][j]=;
    }
    }
    for (int j=; j<N; j++)
    {
    if (get(M-,j)!=) return -;
    }
    int res=;
    for (int i=; i<M; i++)
    for (int j=; j<N; j++)
    res+=j[i[flip]];
    return res;
    }

Fliptile(POJ 3279)的更多相关文章

  1. Enum:Fliptile(POJ 3279)

    Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一 ...

  2. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  3. kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279

    题目链接:https://vjudge.net/problem/POJ-3279 题意:格子有两面,1表示黑色格子,0表示白色格子,奶牛每次可以踩一个格子,踩到的格子和它周围的上下左右格子都会翻面,也 ...

  4. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  5. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  6. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  7. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  8. 【枚举】POJ 3279

    直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...

  9. poj 3279 Fliptile (简单搜索)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Descrip ...

随机推荐

  1. C#LeetCode刷题之#344-反转字符串​​​​​​​(Reverse String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...

  2. JDK8 String类知识总结

    一.概述 java的String类可以说是日常实用的最多的类,但是大多数时候都只是简单的拼接或者调用API,今天决定深入点了解一下String类. 要第一时间了解一个类,没有什么比官方的javaDoc ...

  3. Spring——AOP实现

    Spring实现AOP 1.什么是 AOP AOP (Aspect Orient Programming),直译过来就是 面向切面编程.AOP 是一种编程思想,是面向对象编程(OOP)的一种补充.面向 ...

  4. centos7下安装docker与镜像加速

    1.背景 centos7下安装docker 2.安装 第一步:检查是否为centos7版本 第二步:依赖环境安装 执行如下两个命令: yum -y install gcc yum -y install ...

  5. 【SDOI2009】 HH的项链 - 莫队

    题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...

  6. 【算法•日更•第二十三期】数据结构:two-pointer(尺取法)&莫队

    ▎引入 ☞『例题』 一道十分easy的题: 洛谷P1638 长度为n的序列,m种数 找一个最短区间,使得所有数出现一遍 n≤1e6 ,m≤2e3. ☞『分析』 这道题非常的简单,但是如果不会two-p ...

  7. mycat数据库集群系列之mysql主从同步设置

    最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...

  8. Git的使用方法及IDEA与Git的集成

    一.Git的环境配置 1.Git软件下载 (下载地址:https://git-scm.com/)由于国外的网站下载的超慢可以使用国内的阿里的开源镜像下载(下载地址:https://npm.taobao ...

  9. C++ Templates(1.3 多模板参数 Multiple Template Parameters)

    返回完整目录 目录 1.3 多模板参数 Multiple Template Parameters 1.3.1 为返回类型设置模板参数参数 Template Parameters for Return ...

  10. Hyperledger Fabric【区块链学习一】

    Hyperledger Fabric 学习 什么是区块链 什么是区块链在我们没有接触的时候,只知道它是一个去中心化的存储方式.当我们发生交易,或者动作的时候我们会将记录通知给所有参与者共同维护,达到去 ...