POJ1753 Flip Game(位运算+暴力枚举)
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4 题意:有一个4*4的棋盘,给你它的初始状态,每次可以翻动一个棋子,但翻动的同时也必须翻动它上下左右的另外四个棋子,求将棋盘翻成全黑和全白的最小次数 题解:这题有各种做法,dfs,高斯消元……
然而我比较懒,喜欢直线型思维,所以就打了个状压枚举状态的暴力
很显然,把同一个棋子翻动两次与不翻动没有任何区别
所以从不翻棋子到十六个全翻总共也就是2^16次
完全可以枚举!
然后就糊出来了!
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int sta,ans=,tmp,flag,flag1,a[][],b[][],dx[]= {,,-,},dy[]= {,,,-}; void flip(int sta)
{
int x=,y=;
tmp=;
flag=;
flag1=;
while(sta)
{
if(sta%==)
{
tmp++;
b[x][y]=!b[x][y];
for(int i=; i<=; i++)
{
if(x+dx[i]<=&&x+dx[i]>&&y+dy[i]<=&&dy[i]+y>)
{
b[x+dx[i]][y+dy[i]]=!b[x+dx[i]][y+dy[i]];
}
}
}
y++;
if(y>)
{
x++;
y=;
}
sta>>=;
}
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(b[i][j])
{
flag=;
}
}
}
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(!b[i][j])
{
flag1=;
}
}
}
if(!flag||!flag1)
{
ans=min(ans,tmp);
}
} int main()
{
char c[];
for(int i=; i<=; i++)
{
scanf("%s",c+);
for(int j=; j<=; j++)
{
if(c[j]=='b')
{
a[i][j]=;
}
else
{
a[i][j]=;
}
}
}
for(int sta=; sta<=(<<)-; sta++)
{
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
b[i][j]=a[i][j];
}
}
flip(sta);
}
if(ans==)
{
puts("Impossible");
return ;
}
printf("%d\n",ans);
return ;
}
POJ1753 Flip Game(位运算+暴力枚举)的更多相关文章
- POJ 2531 Network Saboteur 位运算子集枚举
题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...
- C#枚举中的位运算权限分配浅谈
常用的位运算主要有与(&), 或(|)和非(~), 比如: 1 & 0 = 0, 1 | 0 = 1, ~1 = 0 在设计权限时, 我们可以把权限管理操作转换为C#位运算来处理. 第 ...
- C#位运算讲解与示例2
在C#中可以对整型运算对象按位进行逻辑运算.按位进行逻辑运算的意义是:依次取被运算对象的每个位,进行逻辑运算,每个位的逻辑运算结果是结果值的每个位.C#支持的位逻辑运算符如表2.9所示. 运算符号 意 ...
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- POJ 1753 位运算+枚举
题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...
- C#学习笔记-----C#枚举中的位运算权限分配
一.基础知识 什么是位运算? 用二进制来计算,1&2:这就是位运算,其实它是将0001与0010做位预算 得到的结果是 0011,也就是3 2.位预算有多少种?(我们就将几种我们权限中会 ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
随机推荐
- .NET泛型与非泛型的问题
泛型集合通常情况下,建议您使用泛型集合,因为这样可以获得类型安全的直接优点而不需要从基集合类型派生并实现类型特定的成员.下面的泛型类型对应于现有的集合类型:1.List 是对应于 ArrayList ...
- c++ 浅谈 new 的使用
实例用法: 创建对象: class U_Ptr smart; U_Ptr* ptr = new U_Ptr(); class U_Ptr smart(new U_Ptr(p)); int *p = n ...
- 显示等待 之 text_to_be_present_in_element 判断元素是否有xx 文本信息 用法
- zufeoj 花生(The Peanuts)
花生(The Peanuts) 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版] 题目描述 鲁宾逊先生和他的宠物猴,多多,非常喜欢花生.有一天,他们 ...
- win7 网站发布备注
1.更改 .NET Framework 版本(改原设置v2.0 为v4.0) 2.程序池设置 3.基本设置 4.Web.config (debug="false") <sys ...
- C#获取网络状态
/// <summary> /// 获取网络状态 /// </summary> /// <param name="ip">目标IP地址</ ...
- AOP(面向切面编程概念,本文为翻译)
AOP是什么 AOP为Aspect Oriented Programming的缩写.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用 ...
- java - 输入的字符串中是否包含中文
今天和同事在讨论一个问题,需要检查“输入的字符串中是否包含中文”,刚开始想到是用正则表达式,正则表达式中是以[u4e00-u9fa5]来全匹配字符是否是中文,但现在面临的问题是这个字符串中还可能包含英 ...
- cpu,io密集型计算概念
I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...
- List<T>集合使用总结