SDUT 3571 Password 暴力搜索

时间:2023-03-10 03:46:30
SDUT 3571 Password 暴力搜索

这个题如果裸搜肯定超时了

但是我们可以枚举,用初始串的哪一位数字去填目标串的那一位数字

这样就是暴力6!,复杂度很低,然后需要解决过程中经过的点的问题,

因为是从左向右走,所以记录当前光标,

和当前达到的最右端

所以复杂度是O(T*36*(6!)),2000w的复杂度

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <string>
using namespace std;
typedef long long LL;
const int N=1e6+;
const int INF=0x3f3f3f3f;
int vis[N][][];
int f[]= {,,,,,};
int left(int x,int p)
{
int k1=x/f[p]%;
int k2=x/f[p-]%;
return x+(k2-k1)*f[p]+(k1-k2)*f[p-];
}
int right(int x,int p)
{
return left(x,p+);
}
int judge(int x,int y,int l,int r)
{
int ret=;
for(int i=l; i<=r; ++i)
ret+=abs((x/f[i]%)-(y/f[i]%));
return ret;
}
struct Node
{
int x,p,mx,step;
Node() {}
Node(int a,int b,int c,int d)
{
x=a;
p=b;
mx=c;
step=d;
}
};
queue<Node>q;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int s,t;
scanf("%d%d",&s,&t);
q.push(Node(s,,,));
vis[s][][]=++T;
int ans=INF;
while(!q.empty())
{
Node u=q.front();
q.pop();
if(u.step>=ans)continue;
if(u.p>)
{
Node v=u;++v.step;
--v.p;
if(vis[v.x][v.p][v.mx]!=T)
{
vis[v.x][v.p][v.mx]=T;
q.push(v);
}
}
if(u.p<)
{
Node v=u;
++v.p;++v.step;
v.mx=max(v.mx,v.p);
if(vis[v.x][v.p][v.mx]!=T)
{
vis[v.x][v.p][v.mx]=T;
if(judge(v.x,t,v.mx+,)==)
ans=min(ans,judge(v.x,t,,v.mx)+v.step);
q.push(v);
}
}
if(u.p>)
{
Node v=u;++v.step;
v.x=left(v.x,v.p);
if(vis[v.x][v.p][v.mx]!=T)
{
vis[v.x][v.p][v.mx]=T;
if(judge(v.x,t,v.mx+,)==)
ans=min(ans,judge(v.x,t,,v.mx)+v.step);
q.push(v);
}
}
if(u.p<)
{
Node v=u;++v.step;
v.x=right(v.x,v.p);
v.mx=max(v.mx,v.p+);
if(vis[v.x][v.p][v.mx]!=T)
{
vis[v.x][v.p][v.mx]=T;
if(judge(v.x,t,v.mx+,)==)
ans=min(ans,judge(v.x,t,,v.mx)+v.step);
q.push(v);
}
}
}
--T;
printf("%d\n",ans);
} return ;
}