标签:des style color java os io strong for
2 1234 2144 1111 9999
2 4
学会复制粘贴吧,bfs+优先队列,很水
#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
struct node
{
int x,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
int a,b,c,d;
int mark[10000];
int fun(int x,int d)
{
x+=d;
if(x==10)
x=1;
if(x==0)
x=9;
return x;
}
int fun2(int a,int b,int c,int d)
{
return a*1000+b*100+c*10+d;
}
void inti(int x)
{
a=x/1000;b=(x/100)%10;
c=(x/10)%10;d=x%10;
}
int bfs(int s,int t)
{
int x;
priority_queue<node >q;
node cur,next;
cur.x=s;
cur.step=0;
q.push(cur);
memset(mark,0,sizeof(mark));
mark[s]=1;
while(!q.empty())
{
cur=q.top();
q.pop();
if(cur.x==t)
return cur.step;
next.step=cur.step+1;
inti(cur.x);
next.x=x=fun2(fun(a,1),b,c,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(fun(a,-1),b,c,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,fun(b,1),c,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,fun(b,-1),c,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,b,fun(c,1),d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,b,fun(c,-1),d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,b,c,fun(d,1));
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,b,c,fun(d,-1));
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(b,a,c,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,c,b,d);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
next.x=x=fun2(a,b,d,c);
if(!mark[x])
{
mark[x]=1;
q.push(next);
}
}
return 1;
}
int main()
{
int T,s,t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&s,&t);
int ans=bfs(s,t);
printf("%d\n",ans);
}
return 0;
}
hdu 1195 Open the Lock (bfs+优先队列),布布扣,bubuko.com
hdu 1195 Open the Lock (bfs+优先队列)
标签:des style color java os io strong for
原文地址:http://blog.csdn.net/u011721440/article/details/38582221