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的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
随机推荐
- JetBrains IntelliJ IDEA(IJ)v2019.3.3/3.1/3.2/3.4/3.5 for mac/windows/linux 详细安装破解教程
手欠升级了IntelliJ IDEA到2019.3.3,原来的破解不可用,IntelliJ IDEA 2019.3.3破解办法如下,为方便自己使用记录下.======================= ...
- 配置solrcloud
1.1 Zookeeper集群的搭建 1.1.1 前台条件 三个zookeeper实例.Zookeeper也是java开发的所以需要安装jdk. 1.Linux系统 2.Jdk环境. 3.Zo ...
- php面向对象的三大特性——封装、继承、多态
1.封装 目的:为了使类更加安全,类里面的成员变量对外界隐藏 做法: 1)将成员变量变成私有的: 2)在类中做方法间接访问成员变量: 3)在方法里面加控制条件. 例: class ren{ priva ...
- idea2019 jsp页面加载不到静态文件原因No mapping found for HTTP request with URI
最近在使用idea2019 学习ssm,但是发现我在项目引用的静态文件怎么都无法加载出来,找了很久才解决~~ 给上目录结构图: 第一种:使用 ${pageContext.request.context ...
- leetcode236 Lowest Common Ancestor of a Binary Tree
""" Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in ...
- 禁用UpdateOrchestrator重新启动任务
LTSC2019 联网下载更新后,总是用户不使用电脑的时候重启更新系统. 搜索发现是计划任务中\Microsoft\Windows\UpdateOrachestrator重新任务的导致的. 查看这个任 ...
- Linux 命令 - mknod
mknod 创建块设备或者字符设备文件.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 mknod [选项] 设备名 设备类 ...
- 四 Hibernate的一级缓存&事务管理及其配置
持久态对象: 自动更新数据库,原理是一级缓存. 缓存:是一种优化的方式,将数据存入内存,从缓存/内存中获取,不用通过存储源 Hibernate框架中提供了优化手段:缓存,抓取策略 Hibernate中 ...
- 攻防世界--web新手练习区(1)
1. 题目描述:X老师想让小明同学查看一个网页的源代码,但小明却发现鼠标右键不管用了. http://111.198.29.45:53629 通过阅读题目描述分析,我们需要查看源码,但是鼠标右键 ...
- liunx命令用到的
su:切换成root用户 sudo su:普通用户申请root权限 ping命令可以检查linux是否联网 ping www.baidu.com 如图就是联网了 结束ping包括其他linux的指令 ...