CSU-1336--Interesting Calculator(bfs)


Time Limit: 2MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu

Status

Description

There is an interesting calculator. It has 3 rows of buttons.

 

Row 1: button 0, 1, 2, 3, ..., 9. Pressing each button appends that digit to the end of the display.

Row 2: button +0, +1, +2, +3, ..., +9. Pressing each button adds that digit to the display.

Row 3: button *0, *1, *2, *3, ..., *9. Pressing each button multiplies that digit to the display.

 

Note that it never displays leading zeros, so if the current display is 0, pressing 5 makes it 5 instead of 05. If the current display is 12, you can press button 3, +5, *2 to get 256. Similarly, to change the display from 0 to 1, you can press 1 or +1 (but not both!).

 

Each button has a positive cost, your task is to change the display from x to y with minimum cost. If there are multiple ways to do so, the number of presses should be minimized.

Input

There will be at most 30 test cases.The first line of each test case contains two integers x and y(0<=x<=y<=105).Each of the 3 lines contains 10 positive integers (not greater than 105), i.e. the costs of each button.

Output

For each test case, print the minimal cost and the number of presses.

Sample Input

12 256
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
12 256
100 100 100 1 100 100 100 100 100 100
100 100 100 100 100 1 100 100 100 100
100 100 10 100 100 100 100 100 100 100 

Sample Output

Case 1: 2 2
Case 2: 12 3 

Hint

Source

定义三行十列按钮,第一行0-9表示以0-9结尾对应的花费,第二行表示加上0-9对应的花费,第三行表示乘以0-9对应的花费,问从x--y最小的花费是多少,并且输出对应的操作次数
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
int num[5][30];
int x,y;
int vis[2000000],cost[20000000];
struct node
{
	int x,step,cost;
	friend bool operator < (node a,node b)
	{
		if(a.cost>b.cost)
			return true;
		else if(a.cost==b.cost&&a.step>b.step)
			return true;
		else
			return false;
	}
}p,temp;
void bfs()
{
	memset(vis,0,sizeof(vis));
	memset(cost,INF,sizeof(cost));
	priority_queue<node>q;
	while(!q.empty()) q.pop();
	p.x=x;
	p.cost=0;
	p.step=0;
	q.push(p);
	vis[x]=0;
	cost[x]=0;
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		if(p.x==y)
		{
			printf("%d %d\n",p.cost,p.step);
			return ;
		}
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<10;j++)
			{
				if(i==0)
				temp.x=p.x*10+j;
				else if(i==1)
				temp.x=p.x+j;
				else
				temp.x=p.x*j;
				
				temp.step=p.step+1;
				temp.cost=p.cost+num[i][j];
				
				if(temp.x>y) continue;
				if(temp.cost<cost[temp.x])
				{
					cost[temp.x]=temp.cost;
					q.push(temp);
				}
			}
		}
	}
}
int main()
{
	int Case=1;
	while(scanf("%d%d",&x,&y)!=EOF)
	{
		memset(num,0,sizeof(num));
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<10;j++)
			scanf("%d",&num[i][j]);
		}
		printf("Case %d: ",Case++);
		bfs();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值