hdu 3567(八码数 + 双向bfs)
作者:互联网
题目连接:https://vjudge.net/contest/353606#problem/B
参考文章:https://blog.csdn.net/laaahu/article/details/96648344
思路:
广搜,记录路径,因为最多的交换次数不会太多,所以可以用int类型的4进制数记录路径;因为要求字典序最小,所以走得方向是dlru,当每次找到合法的路径时判断是否是字典序最小的路径,因为是双向bfs,每次判断当前路径是否是字典序最小的,不断更新就好了。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 9;
const int MAXN = 4e5 + 10;
const int inf = 1e9+10;
char s1[N + 5], s2[N + 5];
int min_step, vis[2][MAXN],tp[120];
ll pre[2][MAXN], pp[120];
struct Node {
int id, loc, b[N], stu, num;
ll path;
};
char op[5] = "dlru";
string rs;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, -1, 1, 0};
int cantor(int aa[]) {
int ans = 0;
for(int i = 0; i < 9; i++) {
int res = 0, m = 1, c = 1;
for(int j = i + 1; j < 9; j++) {
if(aa[j] < aa[i])
res++;
m *= c;
c++;
}
ans += res * m;
}
return ans + 1;
}
string get_path(ll x,int bj,int k)
{
int tot = 0;
for(int i=1;i<=vis[bj][k];i++)
{
tp[++tot] = x%4; x/=4;
}
string ss = "";
for(int i=tot;i>=1;i--)
{
ss += op[ tp[i] ];
}
return ss;
}
void bfs() {
memset(vis, -1, sizeof(vis));
queue <Node> q;
Node t1, t2;
for(int i = 0; i < 9; i++) {
if(s1[i] == 'X')
t1.b[i] = 9, t1.loc = i;
else
t1.b[i] = s1[i] - '0';
if(s2[i] == 'X')
t2.b[i] = 9, t2.loc = i;
else
t2.b[i] = s2[i] - '0';
}
t1.id = 0;
t1.stu = cantor(t1.b);
t1.path = 0;
vis[0][t1.stu] = 0;
t1.num = 0;
t2.id = 1;
t2.stu = cantor(t2.b);
t2.path = 0;
vis[1][t2.stu] = 0;
t2.num = 0;
q.push(t1);
q.push(t2);
while(!q.empty()) {
t1 = q.front();
q.pop();
ll tmp;
int x = t1.loc / 3, y = t1.loc % 3, id = t1.id;
//printf("x = %d,y = %d,id = %d\n",x,y,id);
for(int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(0 <= tx && tx < 3 && 0 <= ty && ty < 3) {
t2 = t1;
t2.b[x * 3 + y] = t2.b[tx * 3 + ty];
t2.b[tx * 3 + ty] = 9;
//for(int j=0;j<9;j++) printf("%d",t2.b[j]); printf("\n");
t2.loc = tx * 3 + ty;
t2.stu = cantor(t2.b);
t2.num = t1.num + 1;
if(vis[id][t2.stu] == -1) {
vis[id][t2.stu] = t1.num + 1;
if(id == 1)
pre[id][t2.stu] = (3 - i) * pp[t1.num] + t1.path;
else
pre[id][t2.stu] = t1.path * 4 + i;
} else {
if(t2.num > vis[id][t2.stu])
continue;
else {
if(id == 1)
tmp = (3 - i) * pp[t1.num] + t1.path;
else
tmp = t1.path * 4 + i;
if(tmp < pre[id][t2.stu]) {
pre[id][t2.stu] = tmp;
}
}
}
t2.path = pre[id][t2.stu];
if(vis[id ^ 1][t2.stu] != -1) {
string ss = get_path(pre[0][t2.stu], 0, t2.stu) + get_path(pre[1][t2.stu], 1, t2.stu);
int len = ss.length();
if(len < min_step) {
min_step = len;
rs = ss;
} else if(len > min_step) {
cout << min_step << endl;
cout << rs << endl;
return ;
} else {
if(rs.compare(ss) > 0)
rs = ss;
}
}
q.push(t2);
}
}
}
}
int main(void) {
pp[0] = 1;
for(int i = 1; i <= 33; i++)
pp[i] = pp[i - 1] * 4;
int T;
scanf("%d", &T);
for(int pt = 1; pt <= T; pt++) {
scanf("%s%s", s1, s2);
bool fg = false;
for(int i = 0; i < 9; i++) {
if(s1[i] != s2[i])
fg = true;
}
//printf("s1 = %s,s2 = %s\n",s1,s2);
if(fg == false) {
printf("Case %d: 0\n\n", pt);
continue;
}
min_step = inf;
printf("Case %d: ", pt);
bfs();
}
return 0;
}
WA掘机 发布了443 篇原创文章 · 获赞 16 · 访问量 3万+ 私信 关注
标签:hdu,stu,int,t2,t1,bfs,码数,path,id 来源: https://blog.csdn.net/qq_41829060/article/details/104091577