中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=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
题意:就是说有x,y两个数,要从x变到y,现在有三行操作,第一行表示在x后面添加一位数,0,1,2,3,4,5,6,7,8,9依次需要的花费,第二行表示x加上一个数,0,1,2,3,4,5,6,7,8,9依次需要的花费,第三行表示x乘一个数,0,1,2,3,4,5,6,7,8,9依次需要的花费,现在要你求最少的花费,要是有多个最少的花费,那么输出步数最少的......
思路:遇到这种题目,最少花费,最多花费,等等之类的,先考虑最短路、搜索,最后才是dp,这个题目由于是点对点的模式,最短路构图的话,比较麻烦,那么就是搜索了,搜索的话,先看状态数,这个题目里面花费就是状态数,题目说了,花费最多为100000,那么也就是说搜索的状态数最大为100000,那么搜索是可行的。若是搜索的话,可以优先考虑广搜,深搜回溯那东东太耗时,并且曾经在写递归的时候,发现单层递归也大概只能是2万层的样子,多了空间没那么大。
好吧,这个题目考虑到这里,广搜明显可行。搜索的话,得优先更新花费小的状态,那么可以引入优先队列。好吧,初期思考到此为止,编完,发现第二组测试数据过不了.......
仔细思考后,发现会存在这样一个问题,由于我在更新完状态,把状态加入队列的时候,就把状态标记了,下次这个状态数就会被直接过滤掉。这里我事先并不能保证这个状态数更新完的时候,加入队列时的价值是最小的,所以这里是不可以标记的,但是如果不标记,明显会出现很多重复的状态,使得暴栈,暴空间,时间复杂度也明显会大上很多......
我是开了一个记录各个状态最小值的数组,如果状态要进入队列,那么它必须比记录的数值要小.......如此,1600ms过了这个题目。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct ss
{
friend bool operator<(const ss a,const ss b)
{
if(a.k>b.k)
return 1;
else if(a.k==b.k&&a.step>b.step)
return 1;
else
return 0;
}
int x;
int step;
int k;
};
int n,m;
int t[4][15];
int vist1[100005];
int vist[100005];
//queue<int>q1;
void bfs()
{
priority_queue<ss>q;
ss p;
p.k=0;
p.x=n;
p.step=0;
vist[n]=0;
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
if(vist1[p.x]==1)
continue;
vist1[p.x]=1;
if(p.x==m)
{
printf("%d %d\n",p.k,p.step);
return;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<=9; j++)
{
ss p1;
p1.step=p.step+1;
if(i==0)
{
p1.x=p.x*10+j;
}
if(i==1)
{
p1.x=p.x+j;
}
if(i==2)
{
p1.x=p.x*j;
}
p1.k=p.k+t[i][j];
if(p1.x>=0&&p1.x<=100000&&!vist1[p1.x]&&vist[p1.x]>p1.k)
{
vist[p1.x]=p1.k;
//vist[p1.x]=1;
//q1.push(p1.x);
q.push(p1);
}
}
}
}
}
int main()
{
int text=0;
while(scanf("%d%d",&n,&m)>0)
{
for(int i=0; i<3; i++)
{
for(int j=0; j<10; j++)
{
scanf("%d",&t[i][j]);
}
}
//memset(vist,0,sizeof(vist));
for(int i=0;i<=100000;i++)
vist[i]=(1<<29);
memset(vist1,0,sizeof(vist1));
printf("Case %d: ",++text);
bfs();
}
return 0;
}
中南大学oj:1336: Interesting Calculator(广搜经典题目)的更多相关文章
- CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Oj 24260: Lilypad Pond (神奇广搜题,状态搜索)
题目 为了让奶牛们娱乐和锻炼,约翰建造了一个美丽的池塘.这个池塘是矩形的,可以分成M×N个方格.一些格子是坚固得令人惊讶的莲花,还有一些是岩石,其余的只是美丽,纯净,湛蓝的水.贝西正在练习芭蕾舞,她站 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- hdu 1253 胜利大逃亡 (广搜)
题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个 ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS
图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- ubuntu源列表(清华,阿里,官方,选一即可)
Ubuntu的源列表在/etc/apt/sources.list中,替换即可 #清华的源deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial m ...
- 【MySQL】MySQL之MySQL常用的函数方法
MySQL常用函数 本篇主要总结了一些在使用MySQL数据库中常用的函数,本篇大部分都是以实例作为讲解,如果有什么建议或者意见欢迎前来打扰. limit Select * from table ord ...
- AndroidStudio OpenCv的配置,不用安装opencv manager
按照以下操作步骤配置并测试了,没问题. 下载OpenCV sdk for Android,解压(我的解压地址是F:\OpenCV-android-sdk) 1)新建项目项目,取名为Opencvtest ...
- k近邻算法-java实现
最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学python,就在朋友的推荐之下选择了这本书进行学习. 一 . K-近邻算法(KNN)概述 最简单最初级的分 ...
- List遍历Java 8 Streams map() examples
1. A List of Strings to Uppercase 1.1 Simple Java example to convert a list of Strings to upper case ...
- CentOS下配置redis允许远程连接
1. 目的 因为想要学习redis,因此在虚拟机中安装了redis,为了实现在物理机可以访问redis,弄了好久:因此记录下来,以免忘记. 2. 环境 虚拟机:CentOS Linux release ...
- php分享二十四:数组
1:isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会. 2:利用array_filter和strlen快速过滤数组中等于0的值 $path ...
- Spring Security教程(六):自定义过滤器进行认证处理
这里接着上篇的自定义过滤器,这里主要的是配置自定义认证处理的过滤器,并加入到FilterChain的过程. 在我们自己不在xml做特殊的配置情况下,security默认的做认证处理的过滤器为Usern ...
- 如何在Google Play商店发布多个版本apk
原文:http://android.eoe.cn/topic/android_sdk 多种apk的支持是一个特点在Google Play,它允许你发布不同的APKs为你的应用匹配不同尺寸的设备.每个A ...
- POJ 3216 Repairing Company(最小路径覆盖)
POJ 3216 Repairing Company id=3216">题目链接 题意:有m项任务,每项任务的起始时间,持续时间,和它所在的block已知,且往返每对相邻block之间 ...