http://poj.org/problem?id=1753

题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数。

思路:暴力枚举。

一共16个棋子,所以可以用二进制来存储。后来看了一下别人的代码,发现居然可以用异或运算来计算翻转情况,方便了不少。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; int t[] = { //对应翻转情况
, , , ,
, , , ,
, , , ,
, , , ,
}; int ans=;
bool vis[( << )]; struct node
{
int ans;
int step;
}; void bfs()
{
memset(vis, false, sizeof(vis));
queue<node>q;
node p;
p.ans = ans;
p.step = ;
q.push(p);
vis[ans] = true;
while (!q.empty())
{
p = q.front();
q.pop();
if (p.ans == || p.ans == )
{
cout << p.step << endl;
return;
}
for (int i = ; i<; i++)
{
node p1;
p1.step = p.step + ;
p1.ans = p.ans^t[i];
if (vis[p1.ans]) continue;
vis[p1.ans] = true;
q.push(p1);
}
}
cout << "Impossible" << endl;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int cnt = ;
for (int i = ; i<; i++)
{
char ch[];
cin >> ch;
for (int j = ; j<; j++)
{
if (ch[j] == 'w')
{
ans += ( << cnt); //转化为十进制
}
cnt--;
}
}
bfs();
return ;
}

POJ 1753 Flip Game(bfs+位压缩运算)的更多相关文章

  1. POJ 1753 Flip Game (状态压缩 bfs+位运算)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  2. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

  3. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  7. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  8. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  9. poj1753 Flip Game(BFS+位压缩)

    题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...

随机推荐

  1. 远程开关机神器Wake On LAN,免费有中文版

    https://wol.aquilatech.com/ Wake On Lan 又名 aquilaWOL,这是一款免费且开源的图形界面 WOL 软件,有繁体中文界面,可以管理多台电脑和网络设备,支持批 ...

  2. 【剑指offer】斐波那契数列

    一.题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 二.思路: 式子: n=0时,f=0:n=1或者n=2时f=1:否则f=f(n-1)+f(n ...

  3. Unity注意事项

    1.尽量避免出现中文路径 2.做2D游戏时,图片最好要与背景图片保持一定距离 以免重叠

  4. hdu1864最大报销额(01背包)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=187#problem/G 该题要注意的就是每张单子A种类的总和不能大与600,同 ...

  5. windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码

    第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...

  6. [LeetCode] 832. Flipping an Image_Easy

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  7. Rpgmakermv(5) MiniLabel插件介绍

    ============================================================================ Introduction ========== ...

  8. 用python实现websocket请求遇到的问题及解决方法。

    想要实现python的ws库功能,实时获取对方服务器ws协议返回的数据,查了下百度,用如下流程: ws = create_connection("wss://ws.xxxxxxx.info/ ...

  9. Python中type的用法

    目录 描述 语法 用法 type和isinstance Type和Object 描述 python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. ...

  10. Qt中layout()->setSizeConstraint(QLayout::SetFixedSize);崩溃的问题

    编译环境: win764位,vs2008编译器,cbd调试器,qt4.8 背景: 按照<C++ Gui Qt4编程>书中第二章的一个例子(sortDialog)一步步抄完,编译运行,显示不 ...