poj 3279 flip Problem Solution

First flip twice and do not turn it in fact is the same answer is a 01 matrix (although this statement is nonsense - -)

Because before seen a similar problem (like a light bulb instead of bricks was it but can not remember exactly), just remember to change each line must be determined by its operation after a row (which is to some extent a kind of greedy algorithm so I think this problem is not too hard to say the search problem is a greedy title), in fact, well understood, the operation will change each row on row row before good if you study the words of the Bank of chaos so we Research on the next line of each line.

 

On the guidance of this idea, we can easily get to the second row of the inverse operation of the second row, and to the last line, if not all zero, it is clear that there is no other operation, and that is "IMPOSSIBLE", but now the problem is that the first line of it? It is not on the line, it's how to perform the operation? emmm, violence enumerate it, for the first line, seemingly no better way to have the first line of the m-th power of possibilities, then if there is a column of 2 m.

According to this idea vast ground to write the code seems to have no problem, but looked around the Internet found that more structured wording of the others, so they borrow in this first - - (and more complicated place parsing), about me the vast wording, the future can come and ac comparison.

The entire https://www.cnblogs.com/sky-stars/p/11195560.html Great God code (if infringement contact me stand deleted)

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM (X, Y) Memset (X, Y, the sizeof (X))
 #define MAXN 20 is
 the using  namespace STD;
 int Result;
 int n-, m;
 int MP [MAXN] [MAXN]; // original map 
int tmp [MAXN] [MAXN]; // map of the current operation 
int ANS [MAXN] [MAXN]; // optimum map 
int dt [] [ 2 ] = {{- . 1 , 0 }, { . 1 , 0 }, { 0 , 0 }, { 0 - 1 }, { 0 , 1 }};//5个方向
int getcolor(int x,int y)//得到(x,y)的颜色
{
    int cnt=mp[x][y];
    for(int i=0; i<5; i++)
    {
        int tx=dt[i][0]+x;
        int ty=dt[i][1]+y;
        if(tx>=0&&tx<n&&ty>=0&&ty<m)
            cnt+=tmp[tx][ty];
    }
    return cnt%2; 
} 
Int Solve () 
{ 
    int CNT = 0 ;
     for ( int I = . 1 ; I <n-; I ++) // second line check from the need to flip 
        for ( int J = 0 ; J <m; J ++ )
             IF (getColor (I- . 1 , J)) 
                tmp [I] [J] = . 1 ;
     for ( int I = 0 ; I <m; I ++) // check whether the last line are all 0 
        iF (getColor (N- . 1 , I))
             return INF;
     for(int i=0; i<n; i++)//统计翻转次数
        for(int j=0; j<m; j++)
            cnt+=tmp[i][j];
    return cnt;
}
int main()
{
    result=INF;
    cin>>n>>m;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>mp[i][j];
    for(int I = 0 ; I < . 1 << m; I ++) // According to a first line lexicographic order may be inverted so that the enumeration 
    { 
        MEM (tmp, 0 ); // initialize the map 
        for ( int J = 0 ; J <m; ++ J ) 
            tmp [ 0 ] [J] = I >> (M- (J + . 1 )) & . 1 ; // state of the first row 
        int SUM = Solve (); // inverted frequency 
        IF (SUM <Result) / / update the map 
        { 
            Result = SUM; 
            the memcpy (ANS, tmp, the sizeof tmp); //tmp数组复制到ans
        }
    }
    if(result==INF)
        cout<<"IMPOSSIBLE"<<endl;
    else//输出
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
                cout<<ans[i][j]<<" ";
            cout<<endl;
        }
    }
}

So we are talking about for this code in combination with the above ideas, I have a few places for me to think when reading.

1. It uses a number of intermediate inverted tmp array to record each of the block, it is to seek a minimum number of turns, take up a middle bridge

2. With respect to the first line of an enumeration:

A binary displacement to enumerate only a few very clever column 01 (note that it enumerates the number of turns is not the color)

3: About getcolor

This code is the most clever getcolor

There are five ways for each brick reversed, and is driven around four himself, and its color is equal to his own color and number of turns plus more than 2, I think this is not that hard really want to add and I think there are a few up, but it does just fine understanding

 4: The key question is how to solve the core problem of this lexicographical

That's what day3 friends, a greedy (as well as the state of compression?) Title, oh because poj3278 too easy so this is the topic of a fourth questions. Question it, emm ideas is not difficult, but Daniel who really did not ye understand the code you first view, be regarded as a training level of the bar code ( today it is also inextricably linked and water issues )

Guess you like

Origin www.cnblogs.com/hanny007/p/11582155.html