【题目大意】

有一个4x4规格的一个棋盘,现在有16个一面黑一面白的棋子分布在这个棋盘上。

翻转一个棋子能够使它以及它上下左右的四个棋子从黑变白,从白变黑。

现在问你至少要经过多少次操作才能够使得整个棋盘的颜色相同。

【分析】

考虑到是4x4的规模,想到用BFS枚举+判重。

注意题目的内存限制是64MB,如果普通的用一个二维数组记录状态可能会超过内存限制。

考虑位运算,下面给出AC代码。

 #include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
const int INF=0x7fffffff;
using namespace std;
struct map
{
int shu[];//位运算按行存储
int times;//翻转次数
map(){memset(shu,,sizeof(shu));}
}data;
bool hash[(<<)+];//哈希表 bool test(map t);//检测
map change(map t,int x,int y);
void print();//检测函数
int bfs();//广搜
int h(map t);//哈希函数 int main()
{
int i,j;
//文件操作
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
memset(data.shu,,sizeof(data.shu)); //读入数据
for (i=;i<=;i++)
{
if (i!=) getchar(); //读取换行符
for (j=;j<=;j++)
{
char temp;
scanf("%c",&temp);
//注意这里存储是从左向右存储
if (temp=='w') data.shu[i]=(data.shu[i]<<)+;
else data.shu[i]=(data.shu[i]<<)+;
}
}
int ans=bfs();
if (ans==INF) printf("Impossible\n");
else printf("%d",ans);
return ;
}
bool test(map t)//检测函数
{
int cnt=,i,j;
for (i=;i<=;i++) cnt+=t.shu[i];
if (cnt== || cnt==(*)) return ;
return ;
}
int bfs()
{
queue<map>Q;
memset(hash,,sizeof(hash));
int i,j;
data.times=;
hash[h(data)]=;
Q.push(data);//加入队列
while (!Q.empty())
{
map u=Q.front();Q.pop();
if (test(u)) return u.times;
for (i=;i<=;i++)
for (j=;j<=;j++)
{
map v=u;
v=change(v,i,j);
v.times=u.times+;
if (test(v)) return v.times;
if (hash[h(v)]==) continue;//哈希判重
Q.push(v);
hash[h(v)]=;
}
}
return INF;
}
int h(map t)//哈希函数
{
int temp=,i,j;
for (i=;i<=;i++) temp=(temp<<)+t.shu[i];
return temp;
}
//第x行,第y个
map change(map t,int x,int y)
{
t.shu[x]=((t.shu[x])^(<<(-y)));//本身
t.shu[x+]=((t.shu[x+])^(<<(-y)));//上面
t.shu[x-]=((t.shu[x-])^(<<(-y)));//下面
if (y!=) t.shu[x]=((t.shu[x])^(<<((-y)+)));//左边
if (y!=) t.shu[x]=((t.shu[x])^(<<((-y)-)));//右边
return t;
}

【POJ1753】Flip Game的更多相关文章

  1. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  2. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  3. 【Atcoder】ARC088 D - Wide Flip

    [题目]D - Wide Flip [题意]给定n个数字的01序列,要求每次翻转>=k个数字使得全0,求最大的k.n<=10^5 [算法]数学 [题解]有两个角度可以得到等价的结论: 1. ...

  4. 181. Flip Bits【easy】

    181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n  ...

  5. 【Atcoder】ARC 080 F - Prime Flip

    [算法]数论,二分图最大匹配 [题意]有无限张牌,给定n张面朝上的牌的坐标(N<=100),其它牌面朝下,每次操作可以选定一个>=3的素数p,并翻转连续p张牌,求最少操作次数使所有牌向下. ...

  6. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  7. 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示

    前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...

  8. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  9. 【转】Netty那点事(二)Netty中的buffer

    [原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch2-buffer.md 上一篇文章我们概要介绍了Netty的原 ...

随机推荐

  1. VS2012(update3)编译Qt5.1.1 32位静态库debug-and-release版及结果分享

    1. 下载zip源码,我下载的是qt-everywhere-opensource-src-5.1.1.zip这个文件. 2.安装python 3.解压缩qt-everywhere-opensource ...

  2. 【HDOJ】1484 Basic wall maze

    BFS. /* 1484 */ #include <iostream> #include <queue> #include <string> #include &l ...

  3. 构造函数语义学之Default Constructor构建操作

    一.Default Constructor的构建操作 首先大家要走出两个误区: 1).任何class如果没有定义default constructor,就会被合成一个来. 2).便以其合成出来的def ...

  4. 1710: [Usaco2007 Open]Cheappal 廉价回文

    Description 为 了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过这个系统时,牛的名字将被自动读入. 每一头牛的电子名字是一个长度为M (1 & ...

  5. Ganglia监控搭建

    一.Ganglia介绍: Ganglia是一个监控服务器.集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标.Ga ...

  6. 后缀数组:SPOJ SUBST1 - New Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  7. Implement Stack using Queues ——LeetCode

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. 数学概念——G 最大公约数

    G - 数论,最大公约数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  9. POJ 3208 Apocalypse Someday

    题意: 将含有连续的三个6的数称为不吉利数,比如666,1666,6662,但是6266吉利.则666为第一个不吉利数,输入整数n,求第n个不吉利数.(n <= 5*10^7) 解法: 如果是给 ...

  10. javaweb项目的优化 - 几番思念

    简单地来看一个浏览器用户访问的流程: 浏览器->服务器->返回结果显示  这么简单地看,可能想得到的优化手段很少,常见的可能就是优化sql,加快数据库处理:加个缓存,加快返回:使用静态文件 ...