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的个数等于 ...
随机推荐
- mysql 执行批量的sql
<?xml version="1.0" encoding="utf-8" ?> <dbconfig> <!-- 数据库驱动 --& ...
- android中asynctask的使用实例
参考此blog写的非常的好http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html MainActivity.java imp ...
- Java基础知识复习(二)
Java 重写(Override)与重载(Overload) 重写 是子类对父类的允许访问的方法的实现过程进行重新编写,返回值和形参都不能改变,属于编译时多态.即外壳不变,核心重写! 重写的好处在于子 ...
- 1131 Subway Map
题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...
- json数据url传递到php后台
1.js进行encodeURI jsonData = encodeURI(jsonData); 2.php进行urldecode $urldecodeJson = urldecode($json); ...
- Android BindService中遇到的一个小问题
今天在使用BindService的时候遇到个小问题,我希望通过Bindservice获取到这个服务,然后执行服务内的某个自定义方法,如下: if(bindService==null){ Intent ...
- Python多线程-信号量
信号量就是一个线程中有多个线程 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading import ...
- Elasticsearch之插件介绍及安装
ES站点插件(以网页形式展现) 1.BigDesk Plugin (作者 Lukáš Vlček) 简介:监控es状态的插件,推荐![目前不支持2.x] 2.Elasticsearch Head Pl ...
- 【原创】基于UDP广播的局域网Web Window Service日志跟踪小工具
一直感觉Web开发或者windows服务的日志跟踪调试不是很方便 特别是在生产环境服务器上面 目前一般的解决方案是通过各种日志工具把错误信息和调试信息 ...
- spring注解注入属性