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 能状态压缩 能状态转移 能状态压缩的题 ...
随机推荐
- unity全屏截图
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- LeetCode——707 设计链表
题目: 总而言之就是要用C++手撸链表,我的代码: class MyLinkedList { public: /** Initialize your data structure here. */ M ...
- django shell的基本使用
作者:python技术人 博客:https://www.cnblogs.com/lpdeboke/ 在日常工作再发中,经常需要测试一些对象.函数.类...等是否正确,但是如果整体运行项目特别麻烦,并且 ...
- [2019杭电多校第八场][hdu6667]Roundgod and Milk Tea
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题目大意是说n个班级,每个班级有ai人和bi杯茶,每个人只能喝其他班的茶并且只能喝一杯.问最多有 ...
- 2019牛客暑期多校训练营(第四场) - K - number - dp
https://ac.nowcoder.com/acm/contest/884/K 一开始整了好几个假算法,还好测了一下自己的样例过了. 考虑到300的倍数都是3的倍数+至少两个零(或者单独的0). ...
- [环境搭建]-Web Api搭建到IIS服务器后PUT请求返回HTTP Error 405.0 - Method Not Allowed 解决方法 转摘:http://blog.csdn.net/qiujuer/article/details/23827531
尝试使用微软的Web Api,他的确是一个很有意思的东西. 让我体会到了许多的方便,但是我发现部署到IIS服务器上去了后PUT和Delete请求将返回405. 原因是IIS的默认处理程序默认情况下只允 ...
- C# http post请求帮助类
using System; using System.Collections.Specialized; using System.IO; using System.Net; using System. ...
- swift实现线程安全的栈和队列
实现一个线程安全的栈 这里使用数组来存储栈的数据.不足之处在于本例中的Stack可以无限扩容,更好的是初始化时候指定一个最大容量,防止不断扩容申请内存导致内存不够的问题.这里的线程安全使用一个串行队列 ...
- ASE Beta Sprint - backend scrum 4
本次scrum于2019.12.10在sky garden进行,持续10分钟. 参与人: Xin Kang, Zhikai Chen, Lihao Ran, Hao Wang 请假: Ning Jia ...
- xxPay搭建步骤
随着电子商务的发展,移动支付也走进我们的日常生活,就像很多基础业务,如物流.云平台一样,我们现在做什么都离不开她.正因为如此,支付是当下最活跃最前沿的话题,很多流行的技术最早用来实现支付,很多新的业务 ...