【POJ3279】Fliptile(开关问题)

本文介绍了一个程序设计挑战,旨在通过翻转棋盘格子颜色来将所有格子变为白色,采用C++实现算法并优化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一个菜逼的成长。。

挑战程序设计书上的代码。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define mp make_pair
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define exp  2.718281828459
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
inline bool DBread(double &num)
{
    char in;double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-'){IsN=true;num=0;}
    else if(in=='.'){IsD=true;num=0;}
    else num=in-'0';
    if(!IsD){
        while(in=getchar(),in>='0'&&in<='9'){
            num*=10;num+=in-'0';}
    }
    if(in!='.'){
        if(IsN) num=-num;
            return true;
    }else{
        while(in=getchar(),in>='0'&&in<='9'){
                num+=Dec*(in-'0');Dec*=0.1;
        }
    }
    if(IsN) num=-num;
    return true;
}
template <typename T>
inline void write(T a) {
    if(a < 0) { putchar('-'); a = -a; }
    if(a >= 10) write(a / 10);
    putchar(a % 10 + '0');
}
/******************head***********************/
const int maxn = 20;
const int dx[5] = {-1,0,0,0,1};
const int dy[5] = {0,-1,0,1,0};
int M,N;
int tile[maxn][maxn];

int opt[maxn][maxn];    //保存最优解
int flip[maxn][maxn];   //保存中间结果
//查询(x,y)的颜色
int get(int x,int y)
{
    int c = tile[x][y];
    for( int d = 0; d < 5; d++ ){
        int x2 = x + dx[d],y2 = y + dy[d];
        if(0 <= x2 && x2 < M && 0 <= y2 && y2 < N){
            c += flip[x2][y2];
        }
    }
    return c % 2;
}
//求出第一行确定情况下的最小操作次数
//不存在解的话返回-1
int calc()
{
    //求出从第二行开始的翻转方法
    for( int i = 1; i < M; i++ ){
        for( int j = 0; j < N; j++ ){
            if(get(i-1,j) != 0){
                //(i-1,j)是黑色的话,则必须翻转这个格子
                flip[i][j] = 1;
            }
        }
    }
    //判断最后一行是否全白
    for( int j = 0; j < N; j++ ){
        if(get(M-1,j) != 0)
            //无解
            return -1;
    }
    //统计反转的次数
    int res = 0;
    for( int i = 0; i < M; i++ ){
        for( int j = 0; j < N; j++ ){
            res += flip[i][j];
        }
    }
    return res;
}
void solve()
{
    int res = -1;
    //按照字典序尝试第一行的所有可能性
    for( int i = 0; i < 1 << N; i++ ){
        cl(flip,0);
        for( int j = 0; j < N; j++ ){
            flip[0][N-j-1] = i >> j & 1;
        }
        int num = calc();
        if(num >= 0 && (res < 0 || res > num)){
            res = num;
            memcpy(opt,flip,sizeof(flip));
        }
    }
    if(res < 0){
        //无解
        printf("IMPOSSIBLE\n");
    }
    else {
        for( int i = 0; i < M; i++ ){
            for( int j = 0; j < N; j++ ){
                printf("%d%c",opt[i][j],j+1 == N?'\n':' ');
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&M,&N)){
        cl(opt,0),cl(flip,0);
        for( int i = 0; i < M; i++ ){
            for( int j = 0; j < N; j++ ){
                scanf("%d",&tile[i][j]);
            }
        }
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值