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的个数等于 ...
随机推荐
- pmap命令 查看进程用了多少内存
pmap命令 查看进程用了多少内存 用法 pmap [ -x | -d ] [ -q ] pids ...
- java代码=========这个代码还是有问题的。不能实现功能呀
有问题的代码: package com.a.b; import javax.swing.*; import java.awt.Color; import java.awt.event.*; impor ...
- Java中的intern变量的讲解
一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...
- jQuery笔记——Ajax
Ajax 全称为:“Asynchronous JavaScript and XML”(异步 JavaScript 和 XML), 它并不是 JavaScript 的一种单一技术,而是利用了一系列交互式 ...
- Py修行路 python基础 (三)字典
names=["zhang"]names2=["jack","jie"]names.clear()print(names) #清空整个列表 ...
- 第十一章 SpringMvc(待续)
············
- c++builder Active Form
新增的属性.方法刷新一下才可以生成方法的实现.保存按钮不生成,刷新就好了. Refresh Implemention
- In function 'int av_clipl_int32_c(int64_t)': error: 'UINT64_C' was not declared in this scope
cygwin下使用ndk编译jni时遇到的错误: /ffmpeg/include/libavutil/common.h: In function 'int av_clipl_int32_c(int64 ...
- linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒(转载)
一.用select()函数实现非阻塞时的等待时间,用到结构体struct timeval {},这里就不多说了. 二.用gettimeofday()可获得微妙级(0.000001秒)的系统时间,调用两 ...
- 0k6410定时器详细分析
看到一篇很好的博文,分析2410定时器中断的使用的,很详细,和大家分享一下 转载来源于http://www.cnblogs.com/Neddy/archive/2011/07/01/2095176.h ...