POJ 1753 Flip Game【枚举】
题目链接:
http://poj.org/problem?id=1753
题意:
由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色。问最少换多少块,使得最终方格变为全白或者全黑~
分析:
典型的枚举。
这种问题被称为开关问题,关键是要对周围的块翻动对自身的影响进行记录。后一排决定了前一排的最终状态,枚举第一排的情况即可。
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 10, INF = 0x3f3f3f3f;
int a[maxn][maxn];
int m[maxn][maxn], s[maxn][maxn];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};
int check(int row, int color)
{
int cnt = 0;
for(int i = 1; i <= 4; i++){
int tmp = (m[row - 1][i] + s[row - 1][i]) % 2 ;
if((a[row - 1][i] != color && tmp == 0)|| (a[row - 1][i] == color && tmp)){
m[row][i] = 1;
cnt++;
for(int j = 0; j < 4; j++){
s[row + dx[j]][i + dy[j]]++;
}
}
}
return cnt;
}
int solve(int color)
{
int res = INF;
for(int i = 0; i < 16; i++){
memset(s, 0, sizeof(s));
memset(m, 0,sizeof(m));
int cnt = 0;
for(int j = 1; j <= 4; j++){
m[1][j] = (i >> (4- j))&1;
if(m[1][j]){
cnt++;
for(int k = 0; k < 4 ; k++)
s[1+dx[k]][j+dy[k]]++;
}
}
for(int i = 2; i <= 4; i++)
cnt += check(i, color);
if(check(5, color)) cnt = INF;
res = min(res, cnt);
}
return res;
}
int main (void)
{
char t;
int cnt = 0;
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
cin>>t;
if(t == 'b'){
cnt++;
a[i][j] = 0;
}else a[i][j] = 1;
}
}
if(cnt == 16||cnt == 0) return cout<<0<<endl,0;
// cout<<solve(1)<<' '<<solve(0)<<endl;
int res = min(solve(1),solve(0));
if(res == INF) cout<<"Impossible"<<endl;
else cout<<res<<endl;
return 0;
}
POJ 1753 Flip Game【枚举】的更多相关文章
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
随机推荐
- Android 友盟和微信的包冲突:Multiple dex files define Lcom/tencent/a/a/a/a/a;
最近App中有个需求是添加微信支付,就在微信技术官网 http://open.weixin.qq.com,查看一下文档,然后下载SDk,Demo.把SDK集成进项目. 照着微信的文档,把jar包和进来 ...
- python3 进程与线程
1.进程定义 狭义的定义: 进程是正在运行的程序的实例. 广义的定义:进程是一个具有 一定独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.它 ...
- python爬虫---实现项目(二) 分析Ajax请求抓取数据
这次我们来继续深入爬虫数据,有些网页通过请求的html代码不能直接拿到数据,我们所需的数据是通过ajax渲染到页面上去的,这次我们来看看如何分析ajax 我们这次所使用的网络库还是上一节的Reques ...
- python之str (字符型)
用途: 存储少量的数据,+ *int 切片, 其他操作方法 切片还是对其进行任何操作,获取的内容全部是strl类型 存储数据单一 格式: 在python中用引号引起来的就是字符串 '今天吃了没?' 1 ...
- gcc 变量类型大小 练习 远离 cygwin64 需要带dll
/* testmini.c -- very simple test program for the miniLZO library */ #include <stdio.h> #inclu ...
- (转)浅谈trie树
浅谈Trie树(字典树) Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...
- 队列的JS实现及广度优先搜索(BFS)的实现
队列是先进先出(FIFO)的数据结构,插入操作叫做入队,只能添加在队列的末尾:删除操作叫做出队,只能移除第一个元素.在JS中,用数组可以很简单的实现队列.JavaScript实现排序算法 functi ...
- 条款31:将文件间的编译依存关系降至最低(Minimize compilation dependencies between files)
NOTE1: 1.支持“编译依存性最小化”的一般构想是:相依于声明式,不要相依于定义式.基于此构想的两个手段是Handle classes 和 Interface classes. 2.程序库头文件应 ...
- viva correction statements
* List of amendments| No. | Location | Amendments ...
- java多线程之ForkJoinPool
转https://www.cnblogs.com/lixuwu/p/7979480.html 阅读目录 使用 背景:ForkJoinPool的优势在于,可以充分利用多cpu,多核cpu的优势,把一个任 ...