[kuangbin带你飞]专题一 简单搜索 D

166 篇文章 0 订阅
32 篇文章 0 订阅

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”.

http://poj.org/problem?id=3279

题意:

n*m的0 1格子,目标是全变成0
每次反转一个,身边的4个都变成相反的(0变1,1变0)
问最少反转多少个,每个格子如果翻了填1,没翻填0

tip:

乍一看挺恐怖的,首先想到一个格子不可能翻转两次,因为反转两次就变回了原来的样子,就是相当于浪费,那么每个格子就是翻转或者不翻转两种可能,而当第一行确定下来翻转与否的时候,第二行就是确定的,因为第一行每个位置,只可能是他正下方第二行的格子去改变,就是说如果第s行第k列是1,那么第s+1行第k列一定要翻,才能保证最后都是0
所以,只需要枚举第一行的所有状态即可

http://poj.org/problem?id=3279

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 20;
int ans;
int n,m,x[maxn],OT;
int ch[maxn][maxn],AS,an[maxn][maxn];
int a[maxn][maxn],dir[5][2] = {{0,1},{0,0},{0,-1},{-1,0},{1,0}};
void init(){
    for(int i =0 ; i < m ;i++)
        for(int j = 0 ; j < n;j++)
            scanf("%d",&a[i][j]);
    memset(an,false,sizeof(an));
    OT = 300;
}

void do1(){     // 0^0, = 0,0^1 =1,1^0 = 1,0^0 = 0
    for(int i = 0 ; i < m ;i++)
        for(int j = 0 ; j < n ;j++)
            ch[i][j] = a[i][j];
    for(int j = 0 ; j < n ;j++){
        if(x[j] == 0)   continue;
        for(int k = 0 ; k < 5; k++){
                    int xx = 0+dir[k][0],y = j+dir[k][1];
                    if(xx >= 0 && xx < m && y>=0 && y < n)
                        ch[xx][y] ^= 1;
        }
    }
}

int do2(int pp){
    for(int i = 1 ; i < m ;i++){
        for(int j = 0 ; j < n ;j++){
            if(ch[i-1][j]){
                if(pp)
                    an[i][j] = 1;
                ans++;
                for(int k = 0 ; k < 5; k++){
                    int xx = i+dir[k][0],y = j+dir[k][1];
                    if(xx >= 0 && xx < m && y>=0 && y < n)
                        ch[xx][y] ^= 1;
                }
            }
        }
    }
    for(int i = 0 ; i < n ;i++)
        if(ch[m-1][i])  return 0;
    return 1;
}

void sov(){
    for(int sp = 0 ; sp < (1<<n);sp++){
        int k = sp;
        ans = 0;
        for(int i = n-1;i>=0;i--){
            x[i] = k >> 0 & 1;
            if(x[i] == 1)   ans++;
            k >>= 1;
        }
        do1();
        if(do2(0)&&OT>ans){
            OT = ans;
            AS = sp;
        }
    }
    //printf("%d\n",OT);
}

void print(){
    int k = AS;
    for(int i = n-1;i>=0;i--){
            x[i] = k >> 0 & 1;
            if(x[i] == 1)   an[0][i] = 1;
            k >>= 1;
        }
    do1();
    do2(1);
    for(int i = 0 ;i < m ;i++)
        for(int j = 0 ;j < n ;j++)
            printf("%d%c",an[i][j],j == n-1?'\n':' ');
}

int main(){
    while(~scanf("%d%d",&m,&n)&&n){
        init();
        sov();
        if(OT == 300)   printf("IMPOSSIBLE\n");
        else    print();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值