zoukankan      html  css  js  c++  java
  • POJ 3279 Fliptile (dfs+二进制)

    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 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <iostream>
     6 using namespace std;
     7 #define inf 0x3f3f3f3f
     8 int map[16][16],flip[16][16],ans[16][16],preans[16][16];
     9 //preans表示中间的得到的可行的答案,每次比较步数取最优
    10 int n,m;
    11 int calc (int s)
    12 {
    13     for (int i=0;i<m;++i)
    14     {
    15         if (s&(1<<i))
    16         {
    17             preans[0][i]=1;
    18             flip[0][i]^=1;//x^=1由于异或特性完美模拟翻块!!!0^1=1,1^1=0
    19             if (1<n) flip[1][i]^=1;//如果其他相邻块在界内,翻过去
    20             if (i-1>=0) flip[0][i-1]^=1;
    21             if (i+1<m) flip[0][i+1]^=1;
    22         }
    23     }
    24     for (int i=1;i<n;++i)
    25     {
    26         for (int j=0;j<m;++j)
    27         {
    28             if (flip[i-1][j])//如果某个块的正上面的块是1,那么反转这个块
    29             {
    30                 preans[i][j]=1;
    31                 flip[i][j]^=1;
    32                 flip[i-1][j]^=1;
    33                 if (i+1<n) flip[i+1][j]^=1;
    34                 if (j-1>=0) flip[i][j-1]^=1;
    35                 if (j+1<m) flip[i][j+1]^=1;
    36             }
    37         }
    38     }
    39     for (int i=0;i<m;++i)
    40     {
    41         if (flip[n-1][i])
    42         return inf;
    43     }
    44     int res=0;//统计当前情况的步数
    45     for (int i=0;i<n;++i)
    46         for (int j=0;j<m;++j)
    47             res+=flip[i][j];
    48     return res;
    49 }
    50 int main()
    51 {
    52     //freopen("de.txt","r",stdin);
    53     while (~scanf("%d%d",&n,&m))
    54     {
    55         for (int i=0;i<n;++i)
    56             for (int j=0;j<m;++j)
    57             cin>>map[i][j];
    58         int res=inf;
    59         memset(ans,0,sizeof ans);
    60         for (int i=0;i< 1<<m;++i)//用二进制的方法表示状态
    61         {
    62             memcpy(flip,map,sizeof map);
    63             memset(preans,0,sizeof preans);
    64             int temp=calc(i);
    65             if (temp<res)//更新最少的ans状态
    66             {
    67                 res=temp;
    68                 for (int i=0;i<n;++i)
    69                     for (int j=0;j<m;++j)
    70                     ans[i][j]=preans[i][j];
    71             }
    72         }
    73         if (res==inf)
    74         {
    75             printf("IMPOSSIBLE
    ");
    76         }
    77         else
    78         {
    79             for (int i=0;i<n;++i)
    80             {
    81                 for (int j=0;j<m;++j)
    82                 {
    83                     printf("%d",ans[i][j]);
    84                     if (j!=m-1)
    85                     printf(" ");
    86                 }
    87                 printf("
    ");
    88             }
    89         }
    90     }
    91     return 0;
    92 }
    
    1 0 0 1
    1 0 0 1
    0 0 0 0

    讲道理这个题感觉不算dfs,算了无所谓。
    题意就是给你一个n*m的图,1表示黑色,0表示白色,你可以翻转一些块,是这个块和它周围的块(上下左右,如果不出界)1变成0,0变成1。
    问最少几步,能全变成0,输出翻转了那些块,否则IMPOSSIBLE.
    讲一下思路,枚举第一行状态每个块都有翻与不翻两种情况,一共1<<m种状态。
    对于上述每一种状态,接下来怎么做?考虑到每翻转一个块,它的上下左右块都受到影响,先按照这个翻转策略:如果这个块的上面的那个块是1那么
    就翻转这个块。这样思考每一次都能解决上一行的问题。我们最后只要看一下最后一行是否都是0就行了!
    PS:这个题一开始写了发600+ms,结果看还有70ms过的,打开看看用了异或1来模拟状态,后来改了改200+ms过的。
    代码如下:

  • 相关阅读:
    京东POP店铺使用京东物流,如何拦截订单
    京东POP店铺使用京东物流切仓操作方法
    京东评价系统更新190301
    京东考试题目答案,每次顺序都不一样,一气之下,全部复制出来,满分过
    win10 去掉资源管理器左侧的Creative Cloud Files
    java 截取字符串获取子字符串
    在jsp中如何用request中获取后台传来的数据?
    java 中怎样获取input的值
    获取${}中的值? 比如说var a=${date },无法取出date中的值
    ${}
  • 原文地址:https://www.cnblogs.com/agenthtb/p/6033081.html
Copyright © 2011-2022 走看看