POJ1753 枚举

2017年3月18日 | ljfcnyali
题目大意
有44的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使44的正方形变为纯白或者纯黑?

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

题目分析
进行暴力搜索,枚举每一个答案是否可行。

AC代码

/*************************************************************************
    > File Name: POJ1753.cpp
    > Author: ljf-cnyali
    > Mail: ljfcnyali@gmail.com 
    > Created Time: 2017/3/18 11:15:07
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int maxn = 10000;

int n, m, step;

int Map[6][6];

int dt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool flag = false;

void flip(int x, int y) {
    Map[x][y] = 1 - Map[x][y];
    REP(i, 0, 3) {
        int u = x + dt[i][0];
        int v = y + dt[i][1];
        Map[u][v] = 1 - Map[u][v];
    }
}

bool pd() {
    REP(i, 1, 4)
        REP(j, 1, 4)
            if(Map[i][j] != Map[1][1])
                return false;
    return true;
}

void dfs(int x, int y, int now) {
    if(step == now) {
        flag = pd();
        return ;
    }
    if(flag || x == 5)
        return ;
    flip(x, y);
    if(y < 4)
        dfs(x, y + 1, now + 1);
    else
        dfs(x + 1, 1, now + 1);
    flip(x, y);
    if(y < 4)
        dfs(x, y + 1, now);
    else
        dfs(x + 1, 1, now);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    char c;
    REP(i, 1, 4) {
        REP(j, 1, 4) {
            c = getchar();
            if(c == 'b')
                Map[i][j] = 1;
        }
        getchar();
    }
    for(step = 0; step <= 16; ++ step) {
        dfs(1, 1, 0);
        if(flag) {
            break;
        }
    }
    if(flag)
        printf("%d\n", step);
    else
        printf("Impossible\n");
    return 0;
}

本文转自:http://ljf-cnyali.cn/index.php/archives/61

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值