题意: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. 41和为S的连续正数序列+注意循环条件记一下这题特殊解法

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  2. 图解Mybatis框架原理及使用

    1.前言 努力学习完ssm框架之后,终于也成功的把三大框架的使用以及配置文件细节忘得一干二净.为了努力捡起来以及方便今后的复习,决定写一篇博客记录一下. 本博客的所有分析都是在持久层接口以及接口中的方 ...

  3. Http服务和JSP

    需要先安装tomocat8.0,并且使用的IDEA 一个web项目 新建项目 写代码 // 新建一个class @WebServlet("/test") public class ...

  4. 「BJWC2012」冻结

    传送门 Luogu 解题思路 分层图最短路,层与层之间的边的边权减半,然后就是板子了. 细节注意事项 咕咕咕. 参考代码 #include <algorithm> #include < ...

  5. 「SCOI2009」windy数

    传送门 Luogu 解题思路 数位 \(\text{DP}\) 设状态 \(dp[now][las][0/1][0/1]\) 表示当前 \(\text{DP}\) 到第 \(i\) 位,前一个数是 \ ...

  6. 【Unity】鼠标指向某物体,在其上显示物体的名字等等等等信息

    之前一直用NGUI HUD Text插件做这个功能,感觉一个小功能就导一个插件进来简直丧心病狂.然后就自己写了一个~ Camera cam;//用于发射射线的相机 Camera UIcam;//UI层 ...

  7. 【转】深入分析JAVA IO(BIO、NIO、AIO)

    IO的基本常识 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否完成 2.异步 用户触发IO操作以后,可以干别的事,IO操作完成以后再通知当前线程继续处理 3.阻塞 当一个线程调用 r ...

  8. LINQ -- 匿名类型

    匿名类型注意事项: 匿名类型只能和局部变量配合使用,不能用于成员. 由于匿名类型没有名字,我们必须使用var关键字作为变量类型. 不能设置匿名类型对象的属性.编译器为匿名类型穿件的属性是只读的. 除了 ...

  9. 分享一款免费的工控组态软件(PCHMI)

    PCHMI严格的讲它并不是一款组态软件,也不是一款SCADA软件,而是一个基于.NET构架的DLL文件,开发者可以使用微软的Visual Studio将PCHMI.DLL加载到工具箱里面进行二次开发. ...

  10. 图像检索:CEDD(Color and Edge Directivity Descriptor)算法 颜色和边缘的方向性描述符

    颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...