Interesting Calculator

东门文斌
2023-12-01
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
//题意:由x变换成y需要花费的最小代价,变换的方式有一下3种:
          一:可以在原数的末尾加一位数(0....9);
          二:可以在原数的基础上加一个数(0.....9);
          三:可以在原数的基础上乘一个数(0.....9);
//标程:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
struct node
{
    int cost, value, step;
    friend bool operator<(const node a,const node b)
    {
        if(a.cost > b.cost)
           return true;
        else if(a.cost == b.cost && a.step > b.step)
           return true;
        else return false;
    }
    node(int _cost, int _value, int _step)
    {
        cost = _cost;
        value = _value;
        step = _step;
    }
};
int x, y, a[4][11], vis[100010];
void bfs()
{
    int i, j;
    memset(vis,0,sizeof(vis));
    node temp = node(0,x,0);
    priority_queue<node> q;
    q.push(temp);
    while(!q.empty())
    {
        temp = q.top();
        q.pop();
        if(vis[temp.value]) continue;
        vis[temp.value] = 1;
        if(temp.value == y)
        {
            cout << temp.cost << ' ' << temp.step << endl;
            return ;
        }
        for(i = 0; i < 3; ++ i)
        {
            for(j = 0; j <= 9; ++ j)
            {
                node tmp = node(temp.cost+a[i][j],0,temp.step+1);
                if(i == 0)
                    tmp.value = temp.value*10+j;
                else if(i == 1)
                    tmp.value = temp.value + j;
                else if(i == 2)
                    tmp.value = temp.value*j;
                if(tmp.value <= 100000 && !vis[tmp.value])
                    q.push(tmp);
            }
        }

    }
}
int main()
{
//    freopen("a.txt","r",stdin);
    int k = 0;
    while(cin >> x >> y)
    {
        for(int i = 0; i < 3; ++ i)
            for(int j = 0; j <= 9; ++ j)
                cin >> a[i][j];
        printf("Case %d: ",++k);
        bfs();
    }
    return 0;
}

 类似资料:

相关阅读

相关文章

相关问答