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 能状态压缩 能状态转移 能状态压缩的题 ...
随机推荐
- [10期]浅谈SSRF安全漏洞
引子:SSRF 服务端请求伪造攻击 很多web应用都提供从其他服务器上获取数据的功能.使用用户指定的URL,web应用可以从其他服务器获取图片,下载文件,读取文件内容等. 这个功能被恶意使用的话,可以 ...
- js 正则整理
//严格验证身份证格式方法function idCardNo(value){ //验证身份证号方法 var area = { 11: "北京", 12: "天津" ...
- python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理
上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...
- python基础-11 socket,IO多路复用,select伪造多线程,select读写分离。socketserver源码分析
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- github廖雪峰git
gitHub地址: https://github.com/DickyQie/Tool-use/tree/git-learning-document
- 移动端的设备提供了一个事件:orientationChange事件
移动端的设备提供了一个事件:orientationChange事件:https://blog.csdn.net/gong1422425666/article/details/79001836
- -webkit-overflow-scrolling:touch介绍和碰到的坑
1.作用 可控制元素在移动设备有滚动回弹效果,可惯性滚动 2.适应场景 在ios移动端上,设置容器overflow-y:scroll;使容器内元素滚动时,滑动会很卡顿,使用-webkit-overfl ...
- Ubuntu中找不到pip3命令的解决方法
Ubuntu中找不到pip3命令的解决方法 Ubuntu 有 python2 和 python3. 今天使用 Ubuntu 中的 python3 时,想要安装第三方库却发现 pip 指向的是 pyth ...
- 2018-8-10-使用-Resharper-特性
title author date CreateTime categories 使用 Resharper 特性 lindexi 2018-08-10 19:16:51 +0800 2018-4-25 ...
- Lamabda Where Select Find First等区别
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...