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的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
随机推荐
- 阿里P7/P8学习路线图——技术封神之路
一.基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 Java内存模型 内存可见性.重排序.顺序一致性.volatile.锁.final 垃圾回收 内存分配策略.垃圾收集器(G1) ...
- Ubuntu 16.04 LTS下matplotlib安装出错
使用命令sudo pip3 install matplotlib已知报错,用同样的命令安装numpy和opencv却没有,因此重装linux系统两次都没有解决(我是在Vmware中创建的).报错如下: ...
- axios token header response request http拦截器 axios实现登录、拦截、登出
axios token header response request http拦截器 axios实现登录.拦截.登出 一个项目学会前端实现登录拦截 https://github.com/superm ...
- 【C语言项目】贪吃蛇游戏(上)
目录 00. 目录 01. 开发背景 02. 功能介绍 03. 欢迎界面设计 3.1 常用终端控制函数 3.2 设置文本颜色函数 3.3 设置光标位置函数 3.4 绘制字符画(蛇) 3.5 欢迎界面函 ...
- 【2019.6.2】python:json操作、函数、集合、random()等
一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...
- java值的传递
public class Example { String str = new String("good"); char[] ch = {'a','b','c'}; public ...
- OVOO
题目描述: $zhx$有一个棵$n$个点的树,每条边有个权值. 定义一个连通块为一个点集与使这些点连通的所有边(这些点必须连通). 定义一个连通块的权值为这个连通块的边权和(如果一个连通块只包含一个点 ...
- [LOJ] 分块九题 1
https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...
- 16. PLUGINS
16. PLUGINS PLUGINS表提供有关服务器插件的信息. PLUGINS表有以下列: PLUGIN_NAME :用于在诸如INSTALL PLUGIN和UNINSTALL PLUGIN之类的 ...
- 【java面试宝典】一年工作经验者适用
时光转瞬即逝,一年时间已经过去,自己准备跳槽了,请了几天假,面试了几家公司之后,拿到了一份offer,有了一点儿小小的心得,记录下来分享给搞Java的朋友,面试主要是几个部分内容. 很多公司会要求笔试 ...