双向BFS统计
Hdu1195
两个四位密码 问你最少多少步能到达
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + ;
const int gakki = + + + + 1e9;
const int MAXN = 2e5 + , MAXM = 2e5 + ;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
inline void addedge(int u, int v)
{
to[++ed] = v;
nxt[ed] = Head[u];
Head[u] = ed;
}*/
struct node
{
int now[];
int step;
} startpoint, endpoint, cnt, to;
void print(node x)
{
for (int i = ; i <= ; i++)
{
cout << x.now[i];
}
}
int finalans;
bool flag = false;
int vis[][][][];
char s[], e[];
queue<node> que[];
int stepsum[];
node get_change(node x, int aim)
{
node cur = x;
swap(cur.now[aim], cur.now[aim + ]);
return cur;
}
node get_next(node x, int aim, int y)
{
x.now[aim] += y;
if (x.now[aim] > )
{
x.now[aim] = ;
}
else if (x.now[aim] < )
{
x.now[aim] = ;
}
return x;
}
void get_vis(node x, int y)
{
vis[x.now[]][x.now[]][x.now[]][x.now[]] = y;
}
void check(node x, int y)
{
if (vis[x.now[]][x.now[]][x.now[]][x.now[]] == (y ^ ))
{
finalans = min(finalans, stepsum[] + stepsum[] + );
flag = true;
}
else if (vis[x.now[]][x.now[]][x.now[]][x.now[]] == -)
{
que[y].push(x);
get_vis(x, y);
}
}
void doit(int x)
{
while (que[x].front().step == stepsum[x] && que[x].size())
{
cnt = que[x].front();
que[x].pop();
for (int i = ; i <= ; i++)
{
if (i != )
{
to = get_change(cnt, i);
to.step = cnt.step + ;
check(to, x);
}
to = get_next(cnt, i, );
to.step = cnt.step + ;
//cout << x << " ", print(cnt), cout << " ", print(to), cout << endl;
check(to, x);
to = get_next(cnt, i, -);
to.step = cnt.step + ;
//cout << x << " ", print(cnt), cout << " ", print(to), cout << endl;
check(to, x);
}
}
stepsum[x]++;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
flag = false;
scanf("%s", s + ), scanf("%s", e + );
stepsum[] = stepsum[] = ;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
for (int k = ; k <= ; k++)
{
for (int w = ; w <= ; w++)
{
vis[i][j][k][w] = -;
}
}
}
}
finalans = INT_MAX;
for (int i = ; i <= ; i++)
{
while (que[i].size())
{
que[i].pop();
}
}
startpoint.step = , endpoint.step = ;
for (int i = ; i <= ; i++)
{
startpoint.now[i] = s[i] - '';
endpoint.now[i] = e[i] - '';
}
//print(startpoint), print(endpoint);
que[].push(startpoint), que[].push(endpoint);
get_vis(startpoint, ), get_vis(endpoint, );
while (!flag)
{
if (que[].size() > que[].size())
{
doit();
}
else
{
doit();
}
}
printf("%d\n", finalans);
}
return ;
}
//Hdu1195
Hdu1401
8X8的棋盘上有四个棋子 问你能不能在八步之内把一个状态转移到另一一个状态
双向BFS统计的更多相关文章
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
随机推荐
- linux下mysql设置主从
一 主服务器修改 mysql的主从设置主要原理是 主数据库开启日志,并且创建从服务器专属账户,从服务器用该账户,读取到日志进行同步 准备两个mysql数据库(如何安装请查看,linux下mysql安 ...
- golang可见性规则(公有与私有,访问权限)
Go语言没有像其它语言一样有public.protected.private等访问控制修饰符,它是通过字母大小写来控制可见性的,如果定义的常量.变量.类型.接口.结构.函数等的名称是大写字母开头 ...
- 【VS开发】【DSP开发】浅谈Linux PCI设备驱动(一)
要弄清楚Linux PCI设备驱动,首先要明白,所谓的Linux PCI设备驱动实际包括Linux PCI设备驱动和设备本身驱动两部分.不知道读者理不理解这句话,本人觉得这句话很重要,对于PCI.US ...
- 前端数据Mock
参考链接:https://www.clloz.com/programming/front-end/js/2019/05/10/data-mock/?utm_medium=hao.caibaojian. ...
- hbase的hue部署和使用
1.组件版本信息 zookeeper hadoop hbase hue zookeeper-3.4.12 hadoop-3.0.3 hbase-2.1.5 4.4.0 2. ...
- HDU 1260 Tickets (动态规划)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Linux动态链接之GOT与PLT
转载于:http://www.cnblogs.com/xingyun/archive/2011/12/10/2283149.html 我们知道函数名就是一个内存地址,这个地址指向函数的入口.调用函 ...
- Java基础(十)
复习 静态方法与成员方法 //另一个类里的静态和成员方法 public class MyClass { //静态方法 public static void method2() { System.out ...
- Hadoop系列读书笔记
<Hadoop应用架构>是Orilley旗下精品系列的图书 Hadoop序列化 Thrift 不支持内部压缩 不能分片 缺少MapReduce的原生支持 Protocol Buffers ...
- 阿里云服务器挖矿脚本bioset攻击解决
1.问题出现 一大早刚起床,阿里云就给我发了一条短信,提醒我服务器出现紧急安全事件:挖矿程序 阿里云“贴心”地提供了解决方法,不过需要购买企业版的安全服务,本着能自己动手就不花钱原则自己搞了起来 于是 ...