poj 1184 广搜进阶题
起初的想法果然就是一个6000000的状态的表示。
但是后面觉得还是太过于幼稚了。
可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小。
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
using std::swap;
using std::queue;
int const INF = ;
int const N = ;
struct node
{
int num[N];
int pos,step,sta;
}cur,nt;
int vis[N][N][N][N][N][N][N][];
node record[];
int visp[][N]=
{
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
};
char start[N+],end1[N+];
int cnt;
void system()
{
char tmp[];
printf("Press 0 to continue\n");
while(scanf("%s",tmp))
{
if(tmp[]=='')break;
}
}
bool judge(node tmp)
{
return vis[tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.pos][tmp.sta];
}
void CreateVis(node tmp)
{
vis[tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.num[]][tmp.pos][tmp.sta]=;
}
void bfs()
{
for(int i=;i<N;i++)cur.num[i]=i;
cur.pos=cur.sta=cur.step=;
CreateVis(cur);
queue<node> q;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=;i<N;i++)
record[cnt].num[i]=cur.num[i];
record[cnt].step=cur.step;
record[cnt].pos=cur.pos;
record[cnt++].sta=cur.sta;
if(cur.pos>)
{
nt=cur;
nt.step=cur.step+;
swap(nt.num[],nt.num[nt.pos]);
if(!judge(nt))
{
q.push(nt);
CreateVis(nt);
}
}
if(cur.pos<)
{
nt=cur;
nt.pos++;
nt.sta++;
nt.step=cur.step+;
if(!judge(nt))
{
q.push(nt);
CreateVis(nt);
}
nt=cur;
if(nt.sta<)
nt.sta+=;
nt.step=cur.step+;
swap(nt.num[],nt.num[nt.pos]);
if(!judge(nt))
{
q.push(nt);
CreateVis(nt);
}
}
}
}
int Min(int a,int b)
{
return a<b?a:b;
}
int main()
{
cnt=;
memset(vis,,sizeof(vis));
bfs();
while(~scanf("%s %s",start,end1))
{
start[]='\0';
end1[]='\0';
//printf("len=%d\n",len);
for(int i=;i<;i++)
{
start[i]=start[i]-'';
end1[i]=end1[i]-'';
}
int step=,flag,ans=INF;
for(int i=;i<cnt;i++)
{
step=record[i].step;
flag=;
for(int j=;j<N;j++)
{
if(!visp[record[i].sta][j]&&start[record[i].num[j]]!=end1[j])
{
flag=;break;
}
else
step+=abs(start[record[i].num[j]]-end1[j]);
}
if(flag)
ans=Min(ans,step);
}
printf("%d\n",ans);
}
return ;
}
poj 1184 广搜进阶题的更多相关文章
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- POJ 3322(广搜)
---恢复内容开始--- http://poj.org/problem?id=3322 题意:http://jandan.net/2008/01/24/bloxorz.html就是这个鬼游戏 我也是郁 ...
- hdu 1180(广搜好题)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj1426--Find The Multiple(广搜,智商题)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18527 Accepted: 749 ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Eight_pku_1077(广搜).java
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21718 Accepted: 9611 Special ...
- HDU 2267 How Many People Can Survive(广搜,简单)
题目 //一道简单的广搜水题 #include<queue> #include<stdio.h> #include<string.h> #include<al ...
随机推荐
- [PR & ML 1] [Introduction] Informal Basic Concepts
最近还没更完OpenCV又开了新坑,谁教machine learning处在紧急又重要的地位呢.更新的内容总结自Pattern Recognition and Machine Learning by ...
- [DP] LGTB 玩THD (复杂状态DP)
LGTB 玩THD LGTB 最近在玩一个类似DOTA 的游戏名叫THD有一天他在守一座塔,对面的N 个小兵排成一列从近到远站在塔前面每个小兵有一定的血量hi,杀死后有一定的金钱gi每一秒,他都可以攻 ...
- 第12条:考虑实现Comparable接口
CompareTo方法没有在Object中声明,它是Comparable接口中的唯一的方法,不但允许进行简单的等同性比较,而且允许执行顺序比较.类实现了Comparable接口,就表明它的实例具有内在 ...
- 抽象工厂(Abstract Factory)模式
一.抽象工厂(Abstract Factory)模式 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态. 为了方便引进抽象工厂模式,引进一个新概念:产品族(Product Family ...
- [翻译][MVC 5 + EF 6] 7:加载相关数据
原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 1.延迟(Lazy)加载.预先(Eage ...
- 微软自带iscsi客户端对iqn的要求
节点名称:Microsoft iSCSI 发起程序严格遵守为 iSCSI 节点名称指定的规则.这些规则也适用于 Microsoft iSCSI 发起程序节点名称以及发现的任何目标节点名称.构建 iSC ...
- 【仿携程JQuery日期价格表】
今天比较闲所以就花点时间又写了点东西. 相信这种价格表大家不会陌生 现在我就模仿它做一个简单版本的.效果如下 首先需要两个时间控件,我这里用的是HTML5里面的时间控件,这个没限制喜欢用什么就用什么 ...
- Web前端新人笔记之CSS结构和层叠
上一篇文章介绍了如何利用CSS选择器为元素应用各种丰富的样式,每个合法的文档都会生成一个结构树,了解这一点,就能根据元素的祖先.属性.兄弟等元素穿件选择器选择元素. 本篇文章将讨论3中机制之间的关系: ...
- mysql用户管理,权限管理
mysql权限 相关操作: 授予的权限分为四组: 列权限:和表中的一个具体列相关,例如:使用update 语句更新test表中name 列的值 表权限:和一个具体的表的所有数据相关,例如:使用 sel ...
- .net面试题( 转)
描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗?这对于系统设计有什么影响 ...