Interesting Calculator CSU - 1336

题目:

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

思路:

把所能达到的数字当做一个一个点,则起始数字和目标数字便是起点和终点,而给定的那些按钮可以想象成可以走的步数,与是便可以抽象成一个迷宫问题了。简单的bfs即可

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int no;
int w[3][10];
int vis[123456];

struct NUM
{
    int cost;
    int times;
    int num;
    NUM(){}
    NUM(int _cost,int _times,int _num):cost(_cost),times(_times),num(_num){}
    friend bool operator < (NUM a,NUM b)
    {
        if(a.cost!=b.cost)
            return a.cost>b.cost;
        else
            return a.times>b.times;
    }
};

priority_queue<NUM>q;

void init()
{
    for(int i=0;i<100005;i++)
        vis[i]=INF;
    while(!q.empty())
        q.pop();
}

void bfs(int st)
{
    NUM t;
    q.push(NUM(0,0,st));
    vis[st]=1;
    while(!q.empty())
    {
        t=q.top();
        if(t.num==m)
        {
            printf("Case %d: %d %d\n",++no,t.cost,t.times);
            return;
        }
        q.pop();
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(i==0&&t.num*10+j<=m&&t.cost+w[i][j]<vis[t.num*10+j])
                    q.push(NUM(t.cost+w[i][j],t.times+1,t.num*10+j)),vis[t.num*10+j]=t.cost+w[i][j];

                if(i==1&&t.num+j<=m&&t.cost+w[i][j]<vis[t.num+j])
                    q.push(NUM(t.cost+w[i][j],t.times+1,t.num+j)),vis[t.num+j]=t.cost+w[i][j];

                if(i==2&&t.num*j<=m&&t.cost+w[i][j]<vis[t.num*j])
                    q.push(NUM(t.cost+w[i][j],t.times+1,t.num*j)),vis[t.num*j]=t.cost+w[i][j];
            }
        }
    }
}

int main()
{
    no=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)
            {
                scanf("%d",&w[i][j]);
            }
        }
        bfs(n);
    }
    return 0;
}
发布了90 篇原创文章 · 获赞 3 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章