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 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)
  • 代码:

      1 #include <cstdio>
      2 #include <cctype>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <cstring>
      6 #define number s-‘0‘
      7
      8 using namespace std;
      9
     10 const int MAX_N=16;
     11 const int INF=0x3f3f3f3f;
     12 const int dx[5]={-1, 0, 0, 0, 1};
     13 const int dy[5]={0, 1, 0, -1, 0};
     14 int N,M;
     15 int flip[MAX_N][MAX_N], tile[MAX_N][MAX_N], opt[MAX_N][MAX_N];
     16
     17 void read(int &x){
     18     char s;
     19     x=0;
     20     bool flag=0;
     21     while(!isdigit(s=getchar()))
     22         (s==‘-‘)&&(flag=true);
     23     for(x=number;isdigit(s=getchar());x=x*10+number);
     24     (flag)&&(x=-x);
     25 }
     26
     27 void write(int x)
     28 {
     29     if(x<0)
     30     {
     31         putchar(‘-‘);
     32         x=-x;
     33     }
     34     if(x>9)
     35         write(x/10);
     36     putchar(x%10+‘0‘);
     37 }
     38
     39 int calc();
     40 int get(int, int);
     41
     42 int main()
     43 {
     44     read(M);read(N);
     45     for (int i=0; i<M; i++)
     46         for (int j=0; j<N; j++)
     47             read(j[i[tile]]);
     48     int res=-1;
     49     for (int i=0; i< 1<<N; i++)
     50     {
     51         memset(flip, 0, sizeof(flip));
     52         for (int j=0; j<N; j++)
     53         {
     54             flip[0][N-j-1]=i>>j&1;
     55         }
     56         int num=calc();
     57         if (num>=0 && (res<0 || num<res))
     58         {
     59             res=num;
     60             memcpy(opt, flip, sizeof(flip));
     61         }
     62     }
     63     if (res<0) puts("IMPOSSIBLE\n");
     64     else
     65         for (int i=0; i<M; i++)
     66             for (int j=0; j<N; j++)
     67                 printf("%d%c", j[i[opt]], j+1==N? ‘\n‘: ‘ ‘);
     68 }
     69
     70 int get(int x, int y)
     71 {
     72     int c=tile[x][y];
     73     for (int i=0; i<5; i++)
     74     {
     75         int x2=x+dx[i], y2=y+dy[i];
     76         if (0<=x2 && x2<M && 0<=y2 && y2<N)
     77         {
     78             c+=y2[x2[flip]];
     79         }
     80     }
     81     return c % 2;
     82 }
     83
     84 int calc()
     85 {
     86     for (int i=1; i<M; i++)
     87     {
     88         for (int j=0; j<N; j++)
     89         {
     90             if (get(i-1,j)!=0) flip[i][j]=1;
     91         }
     92     }
     93     for (int j=0; j<N; j++)
     94     {
     95         if (get(M-1,j)!=0) return -1;
     96     }
     97     int res=0;
     98     for (int i=0; i<M; i++)
     99         for (int j=0; j<N; j++)
    100             res+=j[i[flip]];
    101     return res;
    102 }

原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9509503.html

时间: 2024-10-09 20:33:34

Fliptile(POJ 3279)的相关文章

Enum:Fliptile(POJ 3279)

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

翻转问题 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 <

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

状态压缩+枚举 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 (二进制枚举+模拟)

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

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