poj3279 kuangbin带你飞 搜索 Fliptile

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

题解:

这道题学到一个很重要的思想,局部枚举定全局枚举,其实这是对问题的实质性分析后,得出的,因为第一行的状态确定后,以后的状态只可能由你这一行或者下一行来决定。这是岁问题的分析之后才会有这样的结果。

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
int n,m;

const int inf=0x3f3f3f3f;
const int MAX_=20;
int a[MAX_][MAX_];
int b[MAX_][MAX_];
int c[MAX_][MAX_];
int best[MAX_][MAX_];
bool ans;
int couns,bestcouns;

void out()
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            printf("%d ",best[i][j]);
        }
        printf("\n");
    }
}
void change(int i,int j)
{
    b[i][j]=1-b[i][j];
    if(i-1>=0)
    {
        b[i-1][j]=1-b[i-1][j];
    }
    if(i+1<n)
    {
        b[i+1][j]=1-b[i+1][j];
    }
    if(j-1>=0)
    {
        b[i][j-1]=1-b[i][j-1];
    }
    if(j+1<m)
    {
        b[i][j+1]=1-b[i][j+1];
    }
}
void calc()
{
    int counts=couns;
    int tempc[MAX_][MAX_];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            b[i][j]=a[i][j];
            tempc[i][j]=c[i][j];
        }
    }
    for(int i=1; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(b[i-1][j])
            {
                change(i,j);
                tempc[i][j]=1;
                counts++;
            }
        }
    }
    int i=n-1,sum=0;
    for(int j=0; j<m; j++)
    {
        sum+=b[i][j];
    }
    if(!sum&&counts<bestcouns)
    {
        bestcouns=counts;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                best[i][j]=tempc[i][j];
            }
        }
    }
}
void changes(int i,int j)
{
    a[i][j]=1-a[i][j];
    if(i-1>=0)
    {
        a[i-1][j]=1-a[i-1][j];
    }
    if(i+1<n)
    {
        a[i+1][j]=1-a[i+1][j];
    }
    if(j-1>=0)
    {
        a[i][j-1]=1-a[i][j-1];
    }
    if(j+1<m)
    {
        a[i][j+1]=1-a[i][j+1];
    }
}
void dfs(int r)
{
    if(r>m)
        return;
    if(r==m)
    {
        calc();
        return;
    }
// for(int i=0;i<m;i++)
// cout<<a[0][i]<<" ";
// cout<<endl;
    dfs(r+1);


    changes(0,r);
    c[0][r]=1;
    couns++;
// for(int i=0;i<m;i++)
// cout<<a[0][i]<<" ";
// cout<<endl;
    dfs(r+1);
    couns--;
    c[0][r]=0;
    changes(0,r);
}
int main()
{
    //freopen("poj3279.txt","r",stdin);
    while(cin>>n>>m&&(n!=-1)&&(m!=-1))
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        bestcouns=inf;
        dfs(0);
        if(bestcouns!=inf)
            out();
        else
            cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}

你可能感兴趣的:(搜索,poj)