POJ1077 Eight —— 正向BFS
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html
代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+;
#define AIM 1 //123456789的哈希值为1 struct node
{
int status;
int s[];
int loc;
char path;
int pre; //pre为上一个操作在队列中的下标,用于输出路径
}; int vis[MAXN], fac[] = { , , , , , , , , };
int dir[][] = { -,, ,, ,-, , };
char op[] = {'u', 'd', 'l', 'r' }; int cantor(int s[]) //获得哈希函数值
{
int sum = ;
for(int i = ; i<; i++)
{
int num = ;
for(int j = i+; j<; j++)
if(s[j]<s[i])
num++;
sum += num*fac[-i];
}
return sum+;
} node que[MAXN];
int front, rear;
int bfs(node now)
{
ms(vis,);
front = rear = ; now.status = cantor(now.s);
vis[now.status] = ;
que[rear++] = now; node tmp;
while(front!=rear)
{
now = que[front++];
if(now.status==AIM)
return front-; int x = now.loc/;
int y = now.loc%;
for(int i = ; i<; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx>= && xx<= && yy>= && yy<=)
{
tmp = now;
tmp.s[x*+y] = tmp.s[xx*+yy]; //交换位置,下同
tmp.s[xx*+yy] = ;
tmp.status = cantor(tmp.s);
if(!vis[tmp.status])
{
vis[tmp.status] = ;
tmp.loc = xx*+yy;
tmp.path = op[i]; //保存操作
tmp.pre = front-; //保存当前结点的状态的上一个状态所在的结点在数组(队列)的下标
que[rear++] = tmp;
}
}
}
}
return -;
} void Print(int i) //利用递归的特点(压栈),输出路径
{
if(i==) return; //到了目的状态,则退出。目的状态的结点在数组(队列)中的下标为0
Print(que[i].pre); //访问上一步
putchar(que[i].path); //输出操作
} int main()
{
char tmp[];
while(gets(tmp))
{
node beg;
int cnt = ;
for(int i = ; tmp[i]; i++)
{
if(tmp[i]==' ') continue;
if(tmp[i]=='x') beg.s[cnt] = , beg.loc = cnt++;
else beg.s[cnt++] = tmp[i]-''; }
int i = bfs(beg);
if(i==-)
puts("unsolvable");
else
Print(i), putchar('\n');
}
}
代码二(推荐):把pre和path放在结构体外,利用当前状态status追溯上一个状态status:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+;
#define AIM 1 //123456789的哈希值为1 struct node
{
int status;
int s[];
int loc;
}; int vis[MAXN], fac[] = { , , , , , , , , };
int dir[][] = { -,, ,, ,-, , };
char op[] = {'u', 'd', 'l', 'r' };
char path[MAXN];
int pre[MAXN]; int cantor(int s[]) //获得哈希函数值
{
int sum = ;
for(int i = ; i<; i++)
{
int num = ;
for(int j = i+; j<; j++)
if(s[j]<s[i])
num++;
sum += num*fac[-i];
}
return sum+;
} queue<node>que;
bool bfs(node now)
{
ms(vis,);
while(!que.empty()) que.pop(); now.status = cantor(now.s);
pre[now.status] = -; //开始状态的上一个状态为-1,用于输出路径时“刹车”
vis[now.status] = ;
que.push(now); node tmp;
while(!que.empty())
{
now = que.front();
que.pop();
if(now.status==AIM) //找到了123456789的状态
return true; int x = now.loc/;
int y = now.loc%;
for(int i = ; i<; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx>= && xx<= && yy>= && yy<=)
{
tmp = now;
tmp.s[x*+y] = tmp.s[xx*+yy]; //交换位置,下同
tmp.s[xx*+yy] = ;
tmp.status = cantor(tmp.s);
if(!vis[tmp.status])
{
vis[tmp.status] = ;
tmp.loc = xx*+yy;
pre[tmp.status] = now.status; //tmp.status的上一个状态为now.status
path[tmp.status] = op[i]; //保存操作
que.push(tmp);
}
}
}
}
return ;
} void Print(int status)
{
if(pre[status]==-) return;
Print(pre[status]); //追溯上一个状态
putchar(path[status]);
} int main()
{
char str[];
while(gets(str))
{
node now;
int cnt = ;
for(int i = ; str[i]; i++)
{
if(str[i]==' ') continue;
if(str[i]=='x') now.s[cnt] = , now.loc = cnt++;
else now.s[cnt++] = str[i]-'';
}
if(!bfs(now))
puts("unsolvable");
else
Print(AIM), putchar('\n');
}
}
POJ1077 Eight —— 正向BFS的更多相关文章
- poj1077(康托展开+bfs+记忆路径)
题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...
- POJ1077 八数码 BFS
BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...
- POJ1077 Eight —— 双向BFS
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html (代码一直在精简完善……) 代码一:两个BFS, 两段代码: 用step控制“你一步, 我一步 ...
- POJ1077 Eight —— 反向BFS
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...
- POJ1077 Eight —— 经典的搜索问题
题目链接:http://poj.org/problem?id=1077 Eight Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- 逆向+两次bfs(UVA 1599)
为什么都说简单好想咧.坦白从宽看了人家的代码,涨了好多姿势,, http://blog.csdn.net/u013382399/article/details/38227917 被一个细节坑了.. 2 ...
- 双向BFS—>NOIP2002 字串变换
如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个队列,交替节点搜索 ...
- BFS && DFS
HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...
- UVa1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)
题目大意: 对于一个n个房间m条路径的迷宫(Labyrinth)(2<=n<=100000, 1<=m<=200000),每条路径上都涂有颜色,颜色取值范围为1<=c&l ...
随机推荐
- bat常用命令,转【http://www.cnblogs.com/yplong/archive/2013/04/02/2996550.html】
1.@它的作用是隐藏它后面这一行的命令本身(只能影响当前行).2.echo中文为“反馈”.“回显”的意思.它其实是一个开关命令,就是说它只有两种状态:打开和关闭.于是就有了echo on和echo o ...
- PHP socket 编程中的超时设置
PHP socket 编程中的超时设置.网上找了半天也没找到.贴出来分享之:设置$socket 发送超时1秒,接收超时3秒: $socket = socket_create(AF_INET,SOCK_ ...
- net3:Calendar控件的使用
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- PatentTips - GPU Support for Blending
Graphics processing units (GPUs) are specialized hardware units used to render 2-dimensional (2-D) a ...
- Mac安装IntelliJ IDEA时快捷键冲突设置
Mac有专门的快捷键,和Linux/Windows的不一样. 下面是发现的一些需要屏蔽的快捷键: 一.搜狗输入法: 暂时没发现有冲突. 二.系统 代码提示:Ctrl+空格(输入法开关) 三.其它 暂无 ...
- PHP用CURL发送Content-type为application/json的HTTP/HTTPS请求
<?php $headers = array( "Content-type: application/json;charset='utf-8'", "Accept: ...
- 关于linter
各类代码都有规则格式检查工具,称之为linter 比如:csslint/jslint/eslint/pylint sumlime提供了一个linter的框架SublimeLinter,在里面可以使用各 ...
- ftp服务器调用出错
因工作需要,需从FTP服务器上下载文件,本地测试OK,服务器上在FTP服务器连接之后卡住,没有异常和提示信息,在打印FTP命令之后,显示:150 Opening data channel for fi ...
- svn简单介绍
版本号控制(Revision control)是维护project蓝图的标准做法,能追踪project蓝图从诞生一直到定案的过程.是一种记录若干文件内容变化.以便将来查阅特定版本号修订情况的系统. 能 ...
- INAPP登陆调用的FB接口
public function login_get (){ $this->load->helper ( 'auth' ); $redirectUrl = $this->input-& ...