POJ - 1753 Flip Game (IDA*)
题意: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*)的更多相关文章
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- OpenJudge/Poj 1753 Flip Game
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
随机推荐
- 新闻网大数据实时分析可视化系统项目——3、Hadoop2.X分布式集群部署
(一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 1.基于Apache厂商的最原始的hadoop版本, 所有发行版均基于这个版本进行改进. 2.基于 ...
- Mybatis入门(六)联查之一对多
上一章说了多对一,很多学生被一个老师教,这一章是一个老师教很多学生 目录基本没有变化只是改了配置文件: 2.配置文件: TeacherMapper接口类: package com.hdlf.dao; ...
- 学习angularJs(1)--引用文件
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js">< ...
- Paper代写:别让段落结尾拉低你的分数
为了达到paper写作格式和字数要求,学生往往会在段末做一件事:总结.都不算是一个很长的段落.本来就写不了多少论证的内容,我们还强制加一个总结句,不仅占用了我们论证的篇幅,而且显得多余(段首的主题句已 ...
- iOS大V博客
王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒. http://onevca ...
- vue 加载静态图片
<img class="headImg" :src="require('../../assets/uploads/'+headImg)" alt=&quo ...
- Linux-initramfs
1. 内核启动问题2. 解决方案2.1 ramdisk(比如initrd)2.2 tmpfs(比如initramfs)2.3 ramdisk vs ramfs2.4 临时文件系统2.4.1 观察tmp ...
- define可变参数,float数据传输
define可变参数 一般在调试打印Debug信息的时候, 需要可变参数的宏. 从C99开始可以使编译器标准支持可变参数宏(variadic macros), 另外GCC也支持可变参数宏, 但是两种在 ...
- Day6 - I - Sticks Problem POJ - 2452
Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented b ...
- mysql悲观锁处理赠品库存超卖的情况
处理库存超卖的情况前,先了解下什么是乐观锁和悲观锁,下面的几篇博客已经介绍的比较详细了,我就不在赘述其原理了 [MySQL]悲观锁&乐观锁 对mysql乐观锁.悲观锁.共享锁.排它锁.行锁.表 ...