POJ 1753 Flip Game (状压+暴力)
题目链接:http://poj.org/problem?id=1753
题意:
给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之),选择的这枚棋子的上下左右都会被翻动(前提是上下左右都可以被翻动)。问最少可以翻动多少颗棋子,让整个棋盘变成全部黑色或者全部白色。
题解:
4*4一共就16个格子,每个格子都可以是翻或者不翻,那么就是216翻法。
所以就可以用状态压缩的方法来解决。
比如说47。 47的二进制是00101111,我们就可以认为(从右往左)第1,2,3,4,6位是我们选择要翻转的棋子,而为0的位是我们不翻的。这样只要把1~16与4*4的坐标做一个映射就可以知道我们要翻那一个棋子了。把全部棋子翻完之后判断一下是不是全部是一种颜色,是就更新ans。
————————————————————
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+;
char input[][];
bool color[][];//0为白,1为黑
void reset()//重新初始化color表
{
for(int i=;i<;i++)
for(int j=;j<;j++)
if(input[i][j] == 'b') color[i][j] = ;
else color[i][j] = ;
}
int cnt(int b)//计算b的二进制中右多少个1。
{
int num = ;
while(b>){
num++;
b&=(b-);
}
return num;
}
void clean(int j)//翻转棋子
{
int x, y;
if(j<&&j>=) x=, y=j;
else if(j<&&j>=) x=, y = j-;
else if(j<&&j>=) x=, y = j-;
else x = , y = j-;
// printf("%d %d %d\n", j, x, y);
color[x][y] = !color[x][y];
if(x->=) color[x-][y] = !color[x-][y];//上
if(x+<) color[x+][y] = !color[x+][y];//下
if(y->=) color[x][y-] = !color[x][y-];//左
if(y+<) color[x][y+] = !color[x][y+];//右
}
int judge()
{
bool flag1=;//全0;
bool flag2=;//全1; //判断是不是为全0
for(int i=;i<;i++){
for(int j=;j<;j++){
if(color[i][j]==) {flag1=;break;}//如果出现了1就break
}
}
if(flag1==) return ;//如果是就直接返回1,即表为全0 //同理判断是不是为全1
for(int i=;i<;i++){
for(int j=;j<;j++){
if(color[i][j] == ) {flag2 = ;break;}
}
}
if(flag2==) return ;
return ;//既不是全0,又不是全1,就返回0.
}
int main() {
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
for(int i=;i<;i++)
scanf("%s", input[i]);
int ans = inf;
// for(int i=0;i<16;i++)
// clean(i);
for(int i=;i<(<<);i++){
reset();
int take = cnt(i);
for(int j = ;j<;j++){
if(i&(<<j)){
clean(j);
}
}
if(judge()){
ans = min(ans, take);
}
}
reset();//最后判断初始的时候是否已经是全0或者全1
if(judge()) ans = ;
if(ans !=inf)
printf("%d\n", ans);
else
printf("Impossible\n");
return ;
}
POJ 1753 Flip Game (状压+暴力)的更多相关文章
- POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4863 Accepted: 1983 Descript ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1324 Holedox Moving (状压BFS)
POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 3279 - Fliptile - [状压+暴力枚举]
题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- HDU 5765 Bonds 巧妙状压暴力
题意:给一个20个点无向连通图,求每条边被多少个极小割集包括 分析:极小割集是边的集合,很显然可以知道,极小割集恰好吧原图分成两部分(这个如果不明白可以用反证法) 然后就是奉上官方题解:http:// ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
随机推荐
- Python笔记(二十九)_模块
模块 在Python中,一个.py文件就是一个模块 if __name__ == '__main__':所有模块都有一个 __name__ 属性,__name__ 的值取决于如何应用模块 run当前文 ...
- Generative Model vs Discriminative Model
In this post, we are going to compare the two types of machine learning models-generative model and ...
- php7.3连接MySQL8.0报错 PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]
报的错误: In Connection.php line : SQLSTATE[HY000] [] The server requested authentication method unknown ...
- elementUi--->实现上传图片效果(upload+formData)
现在谈一下elelmentui中使用Upload 上传通过点击或者拖拽上传文件(图片) <el-upload name="multfile" //上传的文件字段名 cl ...
- JavaSE编码试题强化练习5
1.不使用函数实现字符串的翻转 /** * 1.不使用函数实现字符串的翻转 */ public class TestStringReverse { public static void main(St ...
- SQL基本语法和书写格式
插入 insert [into] 表名 [(列名列表)] values (值列表) insert into 新表名 (列名列表) select 列名列表 from 表名 select 表名.列名 in ...
- hdu6357 Hills And Valleys (最长不下降子序列)
题目传送门 题意: 给你0~9的字符串,问你翻转哪个区间后使得其最长不下降子序列长度最长 思路: 因为字符是0~9,所以我们可以定义一个b数组来枚举L,R, 去和原来的字符串去求最长公共子序列长度,不 ...
- 采用pacemaker+corosync实现postgresql双机热备、高可用方案
环境说明 参照上章已完成postgresql流复制配置,并关闭postgres服务. su - postgres pg_ctl -D /data/postgresql/data/ stop -m fa ...
- a标签实现下载canvas图片
令 a 的 href = canvas.toDataURL("image/png");
- MT41J256M16HA-125 原厂订购 现货销售
作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...