POJ1753 搜索
Flip Game
Description
- 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
wwww wwww
wwww bwww
wwbw bbbw
wwww bwww
目标数字为0或65535.
#include "cstdio"
#include "algorithm"
#include "queue"
#include "cmath"
#include "cstring" #define inf 0x3f3f3f
using namespace std;
int g[];
char s[][];
bool use[];
typedef struct {
int st,s;
}N;
int bfs(int s){
queue<N>q;
N no,ne;
memset(use, false, sizeof(use));
no.st=s,no.s=;
q.push(no);
use[s]=true;
while (q.size()){
no=q.front();
q.pop();
if(no.st==||no.st==){
return no.s;
}
for(int i=;i<;i++){
ne.st=no.st^g[i];
ne.s=no.s+;
if(use[ne.st]){
continue;
}
if(ne.st==||ne.st==){
return ne.s;
}
use[ne.st]= true;
q.push(ne);
}
}
return -;
}
int main()
{
memset(g,, sizeof(g));
for(int i=;i<;i++){//存储16个翻子操作
int n=-i;
g[i]=pow(,n);
if(n+<=n/*+){
g[i]+=pow(,n+);
}
if(n->=n/*){
g[i]+=pow(,n-);
}
if(n+<=){
g[i]+=pow(,n+);
}
if(n->=){
g[i]+=pow(,n-);
}
}
while (scanf("%s",s[])!=EOF){
for(int i=;i<;i++){
scanf("%s",s[i]);
}
int st=;
for(int i=;i<;i++){//将初始状态转为数字
for(int j=;j<;j++){
st<<=;
if(s[i][j]=='b'){
st++;
}
}
}
int x=bfs(st);
if(x==-){
printf("Impossible\n");
}else{
printf("%d\n",x);
}
}
return ;
}
POJ1753 搜索的更多相关文章
- poj1753 bfs+奇偶性减枝//状压搜索
http://poj.org/problem?id=1753 题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右) ...
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- solr_架构案例【京东站内搜索】(附程序源代码)
注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置
在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...
- bzoj1079--记忆化搜索
题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...
- bzoj3208--记忆化搜索
题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...
- Android中通过ActionBar为标题栏添加搜索以及分享视窗
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...
- 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
随机推荐
- CSS文字垂直居中和font-family属性
1.单行文字的垂直居中 单行文字垂直居中比较简单,直接让行高==盒子高度即可. 2.多行文字居中 多行文字居中,只需简单计算一下盒子的上padding即可.计算公式:padding-top=(盒子的高 ...
- docker固定IP地址重启不变
docker固定IP地址重启不变 代码地址 https://github.com/lioncui/docker-static-ip 宿主机IP为 10.6.17.12 docker IP为 10.6 ...
- C# 开发系列(二)
1. 参考文档:http://www.yiibai.com/csharp/csharp_environment_setup.html 2. C# ,ASP.NET HTTP Authorization ...
- SpringMVC轻松学习-注解的使用(三)
根据上一讲的例子,我们下面就注解的使用进行详细说明. 我们采用sprng MVC开发项目时,通常都会采用注解的方式,这样可以大大提高我们的开发效率.实现零配置.下面我们从零开始重新做一个spring ...
- jQuery插件的开发
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- libusb 开发者指南-牛胜超(转)
源:libusb 开发者指南 libusb Developers Guidelibusb 开发者指南 原作者:Johannes Erdfelt翻译者:牛胜超 Table of Contents目录 P ...
- 【View层】界面绘制
[引用]:http://www.jianshu.com/p/c5fc8c6b967a [View层]IOS纯代码绘制界面(一) 字数2303 阅读385 评论2 喜欢16 IOS开发中界面绘制占据了绝 ...
- 1)Javascript设计模式:Module模式
最简单的创建对象方法 function User(name) { this.name = name || '' } User.prototype.say = function() { console. ...
- UVa 10074 - Take the Land
题目大意:和UVa 836 - Largest Submatrix差不多,只需要修改一下数据就可以了. #include <cstdio> #include <cstring> ...
- bzoj3809
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题目大意: Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题 ...