题意:4*4的棋盘摆满棋子,有黑有白,翻转一个棋子的同时也将翻转其上下左右的棋子(翻转后黑变白,白变黑),问使棋盘上所有棋子颜色相同,最少翻转的棋子数。

分析:

1、每个棋子至多翻转1次。翻转偶数次与不翻转结果相同,翻转奇数次与翻转1次结果相同。

2、每个棋子翻转或不翻转,共有216种情况。

3、IDA*搜索。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char pic[5][5];
char tmp[5][5];
int maxn;
bool ok;
bool judge_equal(){
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
if(tmp[i][j] != tmp[0][0]) return false;
}
}
return true;
}
bool judge(int x, int y){
return x >= 0 && x < 4 && y >= 0 && y < 4;
}
void flip(int x, int y){
for(int i = 0; i < 5; ++i){
int tmpx = x + dr[i];
int tmpy = y + dc[i];
if(judge(tmpx, tmpy)){
if(tmp[tmpx][tmpy] == 'b') tmp[tmpx][tmpy] = 'w';
else tmp[tmpx][tmpy] = 'b';
}
}
}
void dfs(int x, int y, int cur){
if(cur == maxn){
ok = judge_equal();
return;
}
if(ok || x == 4) return;
flip(x, y);
if(y < 3) dfs(x, y + 1, cur + 1);
else dfs(x + 1, 0, cur + 1);
flip(x, y);
if(y < 3) dfs(x, y + 1, cur);
else dfs(x + 1, 0, cur);
return;
}
int main(){
for(int i = 0; i < 4; ++i){
scanf("%s", pic[i]);
}
ok = false;
for(maxn = 0; maxn <= 16; ++maxn){
memcpy(tmp, pic, sizeof pic);
dfs(0, 0, 0);
if(ok) break;
}
if(!ok) printf("Impossible\n");
else printf("%d\n", maxn);
return 0;
}

  

POJ - 1753 Flip Game (IDA*)的更多相关文章

  1. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  4. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

  5. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  6. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  7. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  8. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  9. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

随机推荐

  1. day12-Python运维开发基础(推导式、生成器与生成器函数)

    1. 推导式(列表推导式.集合推导式.字典推导式) # ### 推导式 : 通过一行循环判断,遍历出一系列数据的方式是推导式 """ 推导式一共三种: 列表推导式,集合推 ...

  2. Windows中使用QEMU创建树莓派虚拟机

    环境: windows 10 2018-04-18-raspbian-stretch.img 一.下载QEMU 根据你的系统情况,下载相应的版本,并安装完成 https://www.qemu.org/ ...

  3. GoJS简单示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. shiro用ajax方式登录

    用了shiro一段时间了,但是有点受不了它请求登录如果验证不通过直接跳的是loginUrl…所以我想很多人想用ajax实现shiro的登录直接在回调函数里面通过js显示出错信息吧. 今天查了一天的资料 ...

  5. 「POI2015」KIN

    传送门 Luogu 解题思路 想要做这道题,只要会维护区间最大子段和就好了. 而这个可以用线段树维护模板点这里 对于重复的情况,我们可以对每一个位置记一个前驱表示和当前位置种类相同的前一个位置. 然后 ...

  6. postgres 删除外部表

    drop external table if exists tableName;

  7. CCF 201703-4 地铁修建(最小生成树)

    题意:A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁.地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之 ...

  8. Docker + Maven + Docker-compose

    前言: docker:容器化管理 maven:支持docker-maven的插件,通过 mvn clean -Dmaven.test.skip package dockerfile:build 打包命 ...

  9. 【Winform】ProgressBar

    var progressBar1 = new System.Windows.Forms.ProgressBar(); ; progressBar1.Maximum = ; progressBar1.V ...

  10. 第1节 HUE:13、hue的下载以及安装配置

    hue的基本介绍:主要是用于与其他各个框架做整合的,提供一个web界面可以供我们去操作其他的大数据框架可以理解为这个hue就是一个与其他各个框架整合的工具,hue本身不提供任何的功能,所有的功能,都是 ...