poj1184 聪明的打字员(BFS剪枝)
http://poj.org/problem?id=1184
用字符串s存下数字,并把光标位置做一个字符加到s末尾,用map做标记状态是否出现过,然后bfs即可。
不剪枝是过不了的,考虑的两种交换操作都不涉及到2,3,4,5位置,所以这几个位置只能通过up,down来改变。
如果这几个位置是和目标状态是不一样的,那肯定是用up,down操作是最优的,而不会执行left,right操作。
所以这几个位置只有在和目标状态一样时做left,right操作。
#include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;
struct point
{
int step;
string s;
};
string e;
map<string,int> my;
queue<point> q;
int BFS(point st)
{
point t,tt; string ss; int i;
while(!q.empty()) q.pop();
q.push(st); my[st.s]=1;
while(!q.empty())
{
t=q.front(); q.pop(); for(ss=t.s,i=0;i<6;i++)
if(ss[i]!=e[i]) break;
if(i==6) return t.step;
ss=t.s; swap(ss[0],ss[ss[6]-'0']);//Swap0:
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
ss=t.s; swap(ss[5],ss[ss[6]-'0']); //Swap1
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
ss=t.s; if(ss[ss[6]-'0']!='9'&&ss[ss[6]-'0']!=e[ss[6]-'0']) ss[ss[6]-'0']+=1; //Up:
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
ss=t.s; if(ss[ss[6]-'0']!='0'&&ss[ss[6]-'0']!=e[ss[6]-'0']) ss[ss[6]-'0']-=1;//Down:
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
ss=t.s;
if(ss[6]-'0'!=0)//left
{
if(ss[6]!='5') //1 2 3 4位置相同才能移动光标
{
if(ss[ss[6]-'0']==e[ss[6]-'0']) ss[6]-=1;
}
else ss[6]-=1;
}
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
ss=t.s;
if(ss[6]-'0'!=5)//Right
{
if(ss[6]!='0') //1 2 3 4位置相同才能移动光标
{
if(ss[ss[6]-'0']==e[ss[6]-'0']) ss[6]+=1;
}
else ss[6]+=1;
}
if(!my.count(ss))
{
tt.s=ss; tt.step=t.step+1;
q.push(tt); my[ss]=1;
}
}
}
int main(int argc, char *argv[])
{
point st;
while(cin>>st.s>>e)
{
my.clear();
st.s+='0'; st.step=0;
cout<<BFS(st)<<endl;
}
return 0;
}
poj1184 聪明的打字员(BFS剪枝)的更多相关文章
- POJ 1184 聪明的打字员
简直难到没朋友. 双向bfs + 剪枝. 剪枝策略: 对于2--5位置上的数,仅仅有当光标在相应位置时通过swap ,up.down来改变.那么当当前位置没有达到目标状态时,left和right无意义 ...
- hdu_1253_胜利大逃亡(bfs+剪枝)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题意:三维BFS,不解释 题解:DFS+剪枝会超时,裸BFS会超时,BFS+剪枝才能AC,有点伤 ...
- HDU-1226 超级密码 (BFS+剪枝)
Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:密 码是一个C进 ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- 聪明的打字员---poj1184(bfs)
题目链接:http://poj.org/problem?id=1184 分析:首先可以发现有6*10^6种状态,比较多,不过搜索的时候可以去除一些无用的状态, 可以发现一个点的值(2-5)如果想要改变 ...
- codevs 1733 聪明的打字员 (Bfs)
/* Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok */ #include<iostream> #include<cstdio> #include&l ...
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- hdu-1253(bfs+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 思路:简单的bfs,就是要注意剪枝. #include<iostream> #inc ...
随机推荐
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- iOS tableView的图片缓存异步载入
1.建立一个viewController. .h文件实现UIScrollViewDelegate和UITableViewDelegate,并声明ICTableViewDelegate(用来实现图片有缓 ...
- MySQL学习笔记:MySQL: ERROR 1064(42000)
ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL s ...
- 设置改变oracle字符集
修改过密码之后就能以dba的身份进行修改了,不是dba的话在执行修改命令的时候会提示你权限不足. 开始-->运行-->cmd,之后输入:"sqlplus sys/oracle ...
- Javascript的事件委托
在谈js的事件委托之前,先来简单说说js事件的一些基础知识吧. 什么是事件?Javascipt与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器中发生的一些特定的交互瞬间. 什么是事件流?事 ...
- MFC让控件随窗口大小而改变
转载自http://blog.csdn.net/chw1989/article/details/7488711 大小和位置都改变(亲测可行) 1.首先为窗体类添加CRect m_rect,该成员变量用 ...
- (转) Special members
原地址:http://www.cplusplus.com/doc/tutorial/classes2/ Special members [NOTE: This chapter requires p ...
- codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Beautiful Strings Vas ...
- Windows平台字符的存储和输出分析
1. 引言 (写于2011-07-30) 在Windows NT系列的操作系统中最常用的两种字符集是ANSI和Unicode.ANSI是一种泛称,每一个国家或地区的ANSI编码都不一样,比如在Wind ...
- git删除分支
git branch -d branchname删除一个分支需要具备的条件: 1 如果待删除的分支没有upstream branch,那么待删除的分支需要合并到HEAD上,否则需要使用-D强制删除 2 ...