题意:
给两个4位数(都是1-9的数字),第一个是当前密码,第二个是可以开锁的密码,问经过最少多少步转换可以开锁?
转换包括:将其中一位+1(9+1=1),减一(1-1=9),交换相邻两位(第一位和最后一位不是相邻)
思路:双向bfs
起点和终点分别标记为1和2,只要遇到对方走过的就返回,输出最小步数。
注意是一层一层地扩展,不是A走一个B走一个,而是A走一层B走走一层。
// 找密码。双向bfs
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int maxn = 10000+5;
int vis[maxn];
int s,e;
char ch[5];
void add(char &c){
if(c == '9') c = '1';
else c = c + 1;
}
void sub(char &c){
if(c == '1') c = '9';
else c = c - 1;
}
// x 的下一个数
int find_next(int x, int kind){
//转为字符串
int k = 0;
ch[4] &#