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 ...
随机推荐
- HIT 1867 经理的烦恼
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=1867 每次更新时判断是否素数,如果从非素数变成素数就Update(x, 1),如果从素数变成非素数就U ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...
- Stable Matching 稳定匹配 婚姻算法 shapley 算法
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051286.html 稳定匹配问题:有N男N女,每个人对于异性都一个排名,先需要得到一种稳 ...
- Linux简介及Ubuntu安装
Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Linux软件包管理 vim使用 用户及用户组管理 文件权限管理 大牛笔记-www.weix ...
- javascripct数组
定义数组 数组对象用来在单独的变量名中存储一系列的值. 我们使用关键词 new 来创建数组对象.下面的代码定义了一个名为 myArray 的数组对象: var myArray=new Array() ...
- HttpContext.Current多线程调用
1.在web网站的Global中,进行数据量比较大的初始化工作,而为了使用户在页面上能够及时响应,我们在Global中开启了一个线程执行该函数模块. 不过,在线程中用到HttpContext.Curr ...
- 干货:Web应用上线之前程序员应该了解的技术细节
[伯乐在线注]:<Web 应用上线前,程序员应考虑哪些技术细节呢?>这是 StackExchange 上面的一个经典问题贴. 最赞回复有 2200+ 顶,虽然大多数人可能都听过其中大部分内 ...
- 一次Oracle数据迁移
目标数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 源数据库 : Oracle Database 11g Enterpri ...
- Git常用命令汇总
1.初始化相关 git init 初始化仓库 git remove add origin url 添加仓库地址 git remove rm origin 删除仓库地址 git clone 克隆别人的分 ...
- HBase的安装与使用
1.安装 由于还是学习阶段,所以没有在生产环境练习,就在本地建了个虚拟机进行HBase的安装. 下载地址http://www.apache.org/dyn/closer.cgi/hbase/,选择一个 ...