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

因为粗心错了好多次……,尤其是把1<<15当成了65535;

参考博客:http://www.cnblogs.com/kuangbin/archive/2011/07/30/2121677.html

主要思想:

1.如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录。因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一标识。而翻转的操作也可以通过通过位操作来完成。显然当棋盘状态id为0(全白)或65535(全黑)时,游戏结束。

2.对于棋盘的每一个状态,都有十六种操作,首先要判断这十六种操作之后是否有完成的情况,如果没有,则再对这十六种操作的结果分别再进行上述操作,显然这里就要用到队列来存储了。而且在翻转的过程中有可能会回到之前的某种状态,而这种重复的状态是不应该再次入队的,所以维护isVis[i]数组来判断id==i的状态之前是否已经出现过,如果不是才将其入队。如果游戏无法完成,状态必定会形成循环,由于重复状态不会再次入队,所以最后的队列一定会是空队列。

3.由于0^1=1,1^1=0,所以翻转的操作可以通过异或操作来完成,而翻转的位置可以通过移位来确定。

#include<stdio.h>
#include<string.h>
#include<queue> using namespace std; int id,vis[1<<17];
struct node
{
int x,step;
}; int bfs()
{
queue<node>q;
int i;
struct node pos,cur;
if(id==0||id==65535)
return 0; memset(vis,0,sizeof(vis));
pos.step=0;
pos.x=id; q.push(pos);
vis[id]=1; while(!q.empty())
{
cur=q.front();
q.pop();
for(i=0; i<=15; i++)
{
pos.x=(cur.x^(1<<i)); if(i+4<16) pos.x^=(1<<(i+4));
if(i-4>=0) pos.x^=(1<<(i-4));
if(i%4!=0) pos.x^=(1<<(i-1));
if((i+1)%4!=0) pos.x^=(1<<(i+1)); if(vis[pos.x]==0)
{
vis[pos.x]=1;
pos.step=cur.step+1;
q.push(pos);
} if(pos.x==0||pos.x==65535)
return pos.step;
}
} return -1;
}; int main()
{
int i,f,j;
char c;
id=0; for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
id=(id<<1);
scanf("%c",&c);
if(c=='b')
id+=1;
else
id+=0;
}
getchar();
}
f=bfs(); if(f==-1)
printf("Impossible\n");
else
printf("%d\n",f);
}

  

poj 1753 Flip Game 枚举(bfs+状态压缩)的更多相关文章

  1. 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 ...

  2. POJ 1753 Flip Game(bfs+位压缩运算)

    http://poj.org/problem?id=1753 题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数. 思路 ...

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

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

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

    Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...

  5. 枚举 POJ 1753 Flip Game

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

  6. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  7. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  8. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  9. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. nginx 重启命令

    #重启nginx sudo /etc/init.d/nginx restart sudo /etc/init.d/nginx Usage: /etc/init.d/nginx {start|stop| ...

  2. IO笔记

    Java流操作有关的类或接口: Java流类图结构: 1.文件操作时,注意要使用File.separator来代替/分隔符实现跨平台 2.RandomAccessFile类并不是流体系中的一员,但常用 ...

  3. ajax post 时 form数据serialize()

    $.post(UrlAddData, $(".AddForm").serialize(), function (data) { if (data.result) { $.liger ...

  4. thinkphp 使用过程中遇到的一个小函数

    1.实现导出Excel文件,并在导出的文件中显示图片 //导出 public function push(){ $goods_list=M('Dajia')->select(); $data = ...

  5. CentOS7安装nagios并配置出图详解

    目录 开始之前 系统环境 监控内容 所需软件包 台机器,全都按照CentOS7最小化模式安装系统 系统版本号 [root@localhost ~]# cat  /etc/redhat-release ...

  6. Throwing cards away I

    Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the top a ...

  7. Thinkphp3.2.2的上传问题

    学习了Thinkphp3.2.2的上传,报出了FILE: F:\development\yxk\Cord\Library\Think\Upload.class.php LINE: 257错误 这应该是 ...

  8. 【MongoDB】 安装为windows services

    参考  http://stackoverflow.com/questions/2404742/how-to-install-mongodb-on-windows # mongodb.conf # da ...

  9. IOS UIVIEW layer动画 总结(转)

    转发自:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/16306_12.html   IOS UIVIEW layer动画 总结, ...

  10. 第一好用的时间 日期插件(Adding a Timepicker to jQuery UI Datepicker)

          最近在一个项目中用到了My97DatePicker,国人写的一个挺不错的时间选择插件,简单易用,但是在调试静态时却发现此插件必须产生一个iframe标签指向其主页,试了很多种方法都不能去除 ...