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统计的更多相关文章

  1. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  2. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  3. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  4. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  5. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  6. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  8. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  9. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

随机推荐

  1. 【.NET】ASP.Net IE10+ SCRIPT:XXX_doPostBack 未定义

    问题描述 GridView中分页控件,点击分页无反应,Linkbutton点击无反应,打开Web控制台,发现如下错误:SCRIPTXXX:_doPostBack 未定义:查询后得知,是由于.NET F ...

  2. cryptopp 加密库的安装

    今天 在搭建环境的过程中遇到一个问题:C++ 的加密库 crypto在新系统中没有安装,于是百度一下,顺便解决问题 1.开源包下载 下载地址:https://www.cryptopp.com/#dow ...

  3. numpy的logspace产生等比数列

    转载至:https://blog.csdn.net/shenpengjianke/article/details/29356755 上一篇介绍了numpy.linspace用于创建等差数列,现在介绍l ...

  4. 【图像-视频处理】YUV420、YV12与RGB24的转换公式

    bool YV12ToBGR24_Native(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) { if (width ...

  5. mac 上iterm终端连接Linux服务后 中文为乱码问题

    https://www.jianshu.com/p/8b00f71b2177 编辑 ssh 配置vim /etc/ssh/ssh_config修改如下:Host *#SendEnv LANG LC_* ...

  6. solr设置分片和副本

    numShards:分片数 replicationFactor:每个分片下的副本数 maxShardsPerNode:当numShards为1,replicationFactor为3时,maxShar ...

  7. 安装VMWare tools 及安装后/mnt中有hgfs但没共享文件的解决办法

    一.首先是安装VMWare tools打开虚拟机软件,在菜单栏‘虚拟机’子菜单下‘安装VMware Tools' 1.以root身份进入Linux 2.此时把linux的/dev/cdrom设备挂载到 ...

  8. mysql的root密码忘了

    忘记密码:https://www.cnblogs.com/ryanzheng/p/9348723.htmlmy.cnf的[mysqld]下加:skip-grant-tables重启 mysql -ur ...

  9. poj2385(基础DP)

    题目链接:http://poj.org/problem?id=2385 题意:最开始Bessie站在树1下面,每一个单位时间有颗苹果从树1或者树2上落下来.每个单位时间Bessie可以移动一次位置,时 ...

  10. PTA(Advanced Level)1042.Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...