双向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 ...
随机推荐
- H3C 模拟器 pc与sw直连 开启telnet
如图所示 1 在pc上添加虚拟网卡,与上一章节的添加方式相同 配置pc的ip地址 10.17.4.3 255.255.252.0 2 sw设置 <sw1>system-view [sw1] ...
- js Date格式转化
Date格式转化为日期 年-月-日(格式随意修改) GetDateStr(dd, AddDayCount) { dd.setDate(dd.getDate() + AddDayCount)// 获取 ...
- C# List方法中存储的问题
遇到一个bug,抓耳挠塞好久都没有解决,有必要记录一下. 现在我使用了一个多维list. IList<IList<int>> list = new List<IList& ...
- P5019 铺设道路
#include<bits/stdc++.h> using namespace std; ]; ; int main() { cin>>n; ;i<=n;i++) cin ...
- 守护进程,互斥锁, IPC ,Queue队列,生产消费着模型
1.守护进程 什么是守护进程? 进程是一个正在运行的程序 守护进程也是一个普通进程,意思是一个进程可以守护另一个进程,比如如果b是a的守护进程,a是被守护的进程,如果a进程结束,b进程也会随之结束. ...
- 【POJ - 3087】Shuffle'm Up(模拟)
Shuffle'm Up 直接写中文了 Descriptions: 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12, ...
- 字符串转 Boolean 的正确方式
String s1 = "false"; String s2 = "true"; String s3 = "fAlSe"; String s ...
- [python] 在指定目录下找文件
import os # 查找当前目录下所有包含关键字的文件 def findFile(path, filekw): return[os.path.join(path,x) for x in os.li ...
- spark教程(13)-shuffle介绍
shuffle 简介 shuffle 描述了数据从 map task 输出到 reduce task 输入的过程,shuffle 是连接 map 和 reduce 的桥梁: shuffle 性能的高低 ...
- 错误:SyntaxError: identifier starts immediately after numeric literal
转载:http://blog.csdn.net/shalousun/article/details/39995443在用JavaScript时,当你使用一个字符传作为函数的参数常常会看到语法错误,在f ...