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里的 ART、JIT、AOT、Dalvik之间有什么关系?
ART.JIT.AOT.Dalvik之间有什么关系? JIT与Dalvik JIT是"Just In Time Compiler"的缩写,就是"即时编译技术", ...
- IOS应用
下面是这个类的一些功能: 1.设置icon上的数字图标 //设置主界面icon上的数字图标,在2.0中引进, 缺省为0 [UIApplicationsharedApplication].applica ...
- fork后子进程从哪里开始执行
子进程和父进程都从调用fork函数的下一条语句开始执行
- swift学习——枚举
swift枚举 1. 枚举基本语法 enum Method { case Add case Sub case Mul case Div } 也可以使用一种更简单的写法 enum Method1{ ca ...
- CAD交互绘制样条线(网页版)
在CAD设计时,需要绘制样条线,用户可以设置样条线线重及颜色等属性. 主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数.详细说明如 ...
- 制作JPEGImages出现的bug
我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...
- Java后端技术微信交流群!工作、学习、技术、资源等!期待你的加入!
<Java后端技术>专注Java相关技术:SSM.Spring全家桶.微服务.MySQL.MyCat.集群.分布式.中间件.Linux.网络.多线程,偶尔讲点运维Jenkins.Nexus ...
- Mac下复制粘贴的快捷键是什么?随记
刚从window换成Mac OS系统的用户对于一些常用的快捷键一定非常的不习惯,“mac复制粘贴快捷键是什么?”这一简单的问题相信很多刚刚从Windows平台转到Mac平台的用户会问到的问题,因为Ma ...
- Django 1.8.11 REST风格路由
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- [工具]Visual Studio
1,Tab键的使用: 如不说有这样的代码:public Member member { get; set; } 当我们编辑完Member后,按一下Tab键,就能够将光标锁定到member上,等待键盘输 ...