BFS CSU

因为要花费最少,如果花费最少的有多个还要使得步数最少

所以在判断一个数字要不要入队列的时候只要判断这个就可以了

I - Interesting Calculator

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

Submit Status Practice CSU
1336

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 contains10 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 2561 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 112 256100 100 100 1 100 100 100 100 100 100100 100 100 100 100 1 100 100 100 100100 100 10 100 100 100 100 100 100 100 

Sample Output

Case 1: 2 2Case 2: 12 3 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <map>
using namespace std;
#define MAXN 100000 + 100
#define INF 999999999

int cost[MAXN];
int con[MAXN];
int num[15][15];
int x,y;
int cas=1;

void BFS(){
    int i,j;
    queue<int>q;
    //vis[x] = true;
    cost[x] = 0;
    q.push(x);
    while(!q.empty()){
        int tt = q.front();
        //vis[tt] = false;
        q.pop();
        int dx;
        for(i=0;i<3;i++){
            for(j=0;j<10;j++){
                if(i == 0){
                    dx = tt*10+j;
                }
                if(i == 1){
                    dx = tt + j;
                }
                if(i == 2){
                    dx = tt*j;
                }
                if(dx > y){
                    continue;
                }
                if((cost[dx]>cost[tt]+num[i][j]) || (cost[dx]==cost[tt]+num[i][j]&&con[dx]>con[tt]+1)){
                    cost[dx] = cost[tt] + num[i][j];
                    con[dx] = con[tt] + 1;
                    /*if(vis[dx] == false){
                        q.push(dx);
                        vis[dx] = true;
                    }*/
                    q.push(dx);
                }
            }
        }
    }
}

int main(){
    int i,j;

    while(~scanf("%d%d",&x,&y)){
        //memset(vis,false,sizeof(false));
        memset(con,0,sizeof(con));
        for(i=0;i<MAXN;i++){
            cost[i] = INF;
        }
        for(i=0;i<3;i++){
            for(j=0;j<10;j++){
                cin>>num[i][j];
            }
        }
        BFS();
        printf("Case %d: %d %d\n",cas++,cost[y],con[y]);
    }

    return 0;
}
时间: 2024-10-02 07:43:47

BFS CSU的相关文章

CSU-ACM2018暑假集训6—BFS

可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU 2717 Catch That Cow(常规bfs) D:bfs+状态压缩 HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典) E:bfs+优先队列 CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列) F:bfs+优先队列(还有点问题) G:很有意思的bfs CodeFor

CSU 1511: 残缺的棋盘(BFS啊 )

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 Description Input 输入包含不超过10000 组数据.每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同. Output 对于每组数据,输出测试点编号和最少步数. Sample Input 1 1 8 7 5 6 1 1 3 3 2 2 Sample

CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 1336: Interesting Calculator 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

CSU 1726 你经历过绝望吗?两次! [BFS]

Description 4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的"猪坚强".不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川"猪坚强"相比,熊本的猪可没那么幸运,因为它们最终还是没能逃过被送往屠宰场的命运. 我们假设"猪坚强"被困在一个N*M的废墟中,其中"@"表示"猪坚强"的位置,"."表示可

csu 1604 SunnyPig (bfs)

Description SunnyPig is a pig who is much cleverer than any other pigs in the pigpen. One sunny morning, SunnyPig wants to go out of the pigpen to date Mrs. Snail, his beloved. However, it’s terribly tough for a pig to go out of the pigpen because th

csu - 1566: The Maze Makers (bfs)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围一圈,用1表示墙,0表示空,这样就可以表示出一个迷宫,现在就是判断这个迷宫属于4种类型中哪种类型. 参考了 大神博客.构图很难,并且想法跟以往的题都不一样.是一个好题. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

csu 最优对称路径(bfs+记忆化搜索)

1106: 最优对称路径 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 371  Solved: 77[Submit][Status][Web Board] Description 给 一个n行n列的网格,每个格子里有一个1到9的数字.你需要从左上角走到右下角,其中每一步只能往上.下.左.右四个方向之一走到相邻格子,不能斜着走, 也不能走出网格,但可以重复经过一个格子.为了美观,你经过的路径还必须关于“左下-右上”这条对角线对称.下图是一个6x6网

CSU 残缺的棋盘 (BFS)

Description Input 输入包含不超过10000 组数据.每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同. Output 对于每组数据,输出测试点编号和最少步数. Sample Input <span class="sampledata">1 1 8 7 5 6 1 1 3 3 2 2</span> Sample Outp

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>