CSU 1336: Interesting Calculator<BFS+优化><湖南省赛真题>

来源:互联网 发布:windows光盘修复系统 编辑:程序博客网 时间:2024/06/05 05:03

1336: Interesting Calculator
Time Limit: 2 Sec Memory Limit: 128 Mb Submitted: 877 Solved: 214
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
湖南省第九届大学生计算机程序设计竞赛

直接bfs搜索所有的按键,但是要注意优化的地方,不然爆内存,超时,答案错误什么的各种错误出来.
优化一点:
用一个数组存储一下得到当前的value值所需要花费的cost值,那么后面遍历的时候再次遇到该value值时,如果得到的花费值>当前的最小花费值,就舍去,不取该方法,从而降低了时间复杂度和空间复杂度.

#include <cstdio>#include <cstring>#include <cstdlib>#include<stack>#include<queue>#include<map>#include<iostream>using namespace std;int a[10][20];int x,y;struct Node{    int cost,times,value;    friend bool operator < (Node a,Node b)    {        if(a.cost==b.cost)            return a.times>b.times;        return a.cost>b.cost;    }} node;priority_queue<Node> q;int cas=1;int val[100005];void bfs(){    node.cost=0;    node.times=0;    node.value=x;    while(!q.empty()) q.pop();    q.push(node);    val[x]=0;    while(!q.empty())    {        Node tp,tmp=q.top();        q.pop();        if(tmp.value==y)        {            printf("Case %d: %d %d\n",cas++,tmp.cost,tmp.times);            return ;        }        for(int i=0; i<3; i++)            for(int j=0; j<=9; j++)            {                if(i==0)                    tp.value=tmp.value*10+j;                else if(i==1)                    tp.value=tmp.value+j;                else                    tp.value=tmp.value*j;                tp.cost=tmp.cost+a[i][j];                tp.times=tmp.times+1;                if(tp.value<=y&&tp.cost<val[tp.value])                {                    q.push(tp);                    val[tp.value]=tp.cost;                }            }    }}int main(){    while(~scanf("%d %d",&x,&y))    {        for(int i=0; i<100005; i++)            val[i]=999999;        for(int i=0; i<3; i++)            for(int j=0; j<10; j++)                scanf("%d",&a[i][j]);        bfs();    }    return 0;}
阅读全文
2 0