HDU1195 双向BFS(或BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过。
其实这道题只是单向BFS就可以过的,但是为了练算法,所以还是用了双向BFS来写。
算法:
先预处理一下,从1111到9999的所有点进行构图(由于是1~9的,所以除去含有0元素的数字),能进行一次变换变成的数字则表示两点之间连通。然后从初态与目态两个点进行BFS,如果有轨迹重合的就返回路程和。
这里注意双向BFS要一层一层的进行搜索,不然的话会产生错误,至于错误原因还在思考中。。
双向BFS代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
vector <int> e[maxn];
int vis[maxn] , dist[maxn]; void solve(int x)
{
int num[] , i , tmp , y;
i = ;
tmp = x;
while(tmp) {
num[i++] = tmp % ;
tmp /= ;
}
for(i = ; i < ; i++)
if(num[i] == )
return;
for(i = ; i < ; i++) {
if(i < ) {
swap(num[i] , num[i + ]);
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
swap(num[i] , num[i + ]);
}
tmp = num[i];
if(num[i] == )
num[i] = ;
else
num[i]++;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp; if(num[i] == )
num[i] = ;
else
num[i]--;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp;
}
}
int BFS_2(int start , int end)
{
if(start == end)
return ;
memset(vis , , sizeof(vis));
queue <int> que[];
vis[start] = ;
vis[end] = ;
que[].push(start);
que[].push(end);
dist[start] = dist[end] = ;
while(!que[].empty() && !que[].empty()) {
int k = ;
if(que[].size() < que[].size())
k++;
int u = que[k].front();
que[k].pop();
for(int i = ; i < e[u].size() ; i++) {
int j = e[u][i];
if(!vis[j]) {
vis[j] = vis[u];
que[k].push(j);
dist[j] = dist[u] + ;
} else if(vis[j] == vis[u]) {
continue;
} else {
return dist[j] + dist[u] + ;
}
}
}
return -;
}
int main()
{
int T , a , b;
for(int i = ; i <= ; i++)
solve(i);
cin >> T;
while(T--) {
scanf("%d %d" , &a , &b);
printf("%d\n" , BFS_2(a , b));
}
return ;
}
BFS代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
vector <int> e[maxn];
int vis[maxn] , dist[maxn]; void solve(int x)
{
int num[] , i , tmp , y;
i = ;
tmp = x;
while(tmp) {
num[i++] = tmp % ;
tmp /= ;
}
for(i = ; i < ; i++)
if(num[i] == )
return;
for(i = ; i < ; i++) {
if(i < ) {
swap(num[i] , num[i + ]);
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
swap(num[i] , num[i + ]);
}
tmp = num[i];
if(num[i] == )
num[i] = ;
else
num[i]++;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp; if(num[i] == )
num[i] = ;
else
num[i]--;
y = num[] * + num[] * + num[] * + num[];
e[x].push_back(y);
e[y].push_back(x);
num[i] = tmp;
}
}
int BFS(int a , int b)
{
if(a == b)
return ;
memset(vis , , sizeof(vis));
queue <int> que;
que.push(a);
vis[a] = ;
dist[a] = ;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = ; i < e[u].size() ; i++) {
int j = e[u][i];
if(j == b)
return dist[u] + ;
if(!vis[j]) {
dist[j] = dist[u] + ;
vis[j] = ;
que.push(j);
}
}
}
}
int main()
{
int T , a , b;
for(int i = ; i <= ; i++)
solve(i);
cin >> T;
while(T--) {
scanf("%d %d" , &a , &b);
printf("%d\n" , BFS(a , b));
}
return ;
}
HDU1195 双向BFS(或BFS)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ1475 Pushing Boxes(BFS套BFS)
描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...
- UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)
题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- 2601 电路维修 (双端队列bfs\优先队列bfs(最短路))
描述 Ha'nyu是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上.Rika的家里有一辆飞行车.有一天飞行车的电路板突然出现了故障,导致无法启动. 电路板 ...
- 【NOIP2013】 华容道 bfs预处理+bfs
这一题我们考虑一个最裸的算法: 我们设$dp[i][j][k][l]$表示当前棋子在$(i,j)$且空格在$(k,l)$时的最小步数 然后显然随便转移一下就好了,时间复杂度为$O(q(nm)^2)$. ...
- Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- BFS:HDU3085-Nightmare Ⅱ(双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- docker17.09.1-ce 执行systemctl resart docker重启失败的问题
记录在实际操作中碰到的docker问题 环境信息: 安装完kolla ocata版本,并且编译成功各openstack 组件的容器镜像 [root@localhost ~]# docker --ver ...
- vue -- 插件的开发与使用
开发插件 插件通常会为 Vue 添加全局功能.插件的范围没有限制--一般有下面几种: 1.添加全局方法或者属性,如: vue-custom-element. 2.添加全局资源:指令/过滤器/过渡等,如 ...
- [Django笔记] uwsgi + nginx 配置
django 和 nginx 通过 uwsgi 来处理请求,类似于 nginx + php-fpm + php 安装nginx 略 安装配置uwsgi pip install uwsgi 回想php- ...
- 洛谷P3145 [USACO16OPEN]分割田地Splitting the Field
P3145 [USACO16OPEN]分割田地Splitting the Field 题目描述 Farmer John's NN cows (3 \leq N \leq 50,0003≤N≤50,00 ...
- DNS 网关 路由 交换机 网桥 协议 服务器 这些都是什么关系?
来源:知乎 服务器:为你提供服务的机器.相当于马路边上的各种店面.虽然理论上任何一户人家都能开店为你提供服务,但是因为各种硬件资源限制而不适合开店.比如:小区道路比较窄(宽带带宽比较窄).家里地方太小 ...
- 解决Maven项目中jar包依赖冲突问题
版本冲突的解决方案 [1]调节原则 [1]路径最短者优先原则 [2]路径相同时,先声明者优先原则 [2]排除原则:用于排除某项依赖的依赖jar包 <dependency> <grou ...
- CODEVS 3027 线段覆盖2
首先,先看题.....(虽然比较简单 3027 线段覆盖 2 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐 ...
- Jmeter 自动化测试报告扩展(转 Todo 需要修正)
首先了解下生成测试报告的过程,我们看到的测试报告是由.jtl格式转换为.html,html报告的样式由extras目录下xsl文件决定.优化测试报告需要分为两部分内容,首先我们要优化输出的测试内容,其 ...
- [Leetcode]008.String to Integer (atoi)
public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...
- vim配置成c++IDE
mv ~/.vimrc ~/.vimrcbak mv ~/.vim ~/.vimbak git clone https://github.com/handy1989/vim.git mv vim/.v ...