记录编号 320902 评测结果 AAAAAAAAAA
题目名称 表达式 最终得分 100
用户昵称 GravatarRapiz 是否通过 通过
代码语言 C++ 运行时间 4.275 s
提交时间 2016-10-12 22:51:01 内存使用 0.31 MiB
显示代码纯文本
#include<cstdio>
#include<map>
#include<queue>
#include<algorithm>
#include<cstring>
#define file(x) "expressb."#x
//#define PRODUCE
using std::queue;
using std::min;
using std::max;
int n,k;
struct NODE{int a,stp;
NODE(int x,int y):a(x),stp(y){
};
};
int gsum(int a){
	int ret=0;
	while(a) ret+=a%10,a/=10;
	return ret;
}
int gmx(int a){
	int ret=0;
	while(a) ret=max(ret,a%10),a/=10;
	return ret;
}
int gmn(int a){
	int ret=1<<30;
	while(a) ret=min(ret,a%10),a/=10;
	return ret;
}
int op(int a,int b){
	return gsum(a)*gmx(b)+gmn(b);
}
void show(int ls,int it,int stp){
	#ifdef PRODUCE
	printf("%d # %d = %d * %d + %d = %d (%d)\n",ls,it,gsum(ls),gmx(it),gmn(it),op(ls,it),stp);
	#endif
}
int d[740];
int solve(){
	scanf("%d%d",&n,&k);
	if(n==k) return 0;
	memset(d,0x3f,sizeof(d));
	queue<int> q;
	q.push(n);
	while(!q.empty()){
		int u=q.front();q.pop();
		int dis;
		if(u==n) dis=0;
		else dis=d[u];
		for(int i=0;i<=739;i++) if(d[i]!=0x3f3f3f3f){
			int t=op(i,u);
			if(d[i]+dis+1<d[t]) d[t]=dis+d[i]+1,q.push(t);
			t=op(u,i);
			if(d[i]+dis+1<d[t]) d[t]=dis+d[i]+1,q.push(t);
		}
		int t=op(u,n);
		if(dis+1<d[t]) d[t]=dis+1,q.push(t);
		t=op(n,u);
		if(dis+1<d[t]) d[t]=dis+1,q.push(t);
	}
	return d[k]==0x3f3f3f3f?-1:d[k];
}
int main(){
	freopen(file(in),"r",stdin);
	freopen(file(out),"w",stdout);
	int T;scanf("%d",&T);
	while(T--) printf("%d\n",solve());
}