中南大学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) ...
随机推荐
- ROS学习(六)—— 理解ROS节点
一.准备工作 下载一个轻量级的模拟器 sudo apt-get install ros-kinetic-ros-tutorials 二.图概念的理解 1.Nodes:一个节点就是一个可执行文件,用来与 ...
- php如何在某个时间上加一天?一小时? 时间加减(转)
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
- MVC2 扩展Models和自定义验证(学习笔记)
当我们利用Visual Studio生成实体类以后,难免会用到验证功能(例如,用户登录时验证用户名是否为空,并加以显示). Visual Studio实体类:实体类 如果直接去编辑Visual Stu ...
- Spring MVC freemarker使用
什么是 FreeMarker? FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具. 它不是面向最终用 ...
- C++转型操作符
转:http://www.cnblogs.com/hazir/archive/2012/04/14/2447251.html 旧式的C转型方式,几乎允许你将任何类型转换为任何其它类型,有其自身的缺陷, ...
- Oracle 12C -- Unified Auditing Policy
1.审计策略是一组审计选项,用来审计数据库用户 2.创建审计策略需要被授予audit_admin角色(create audit policy ...) 3.可以在CDB.PDB级别创建创建审计策略 4 ...
- SharePoint中在线编辑文档
我一直以为只有在Document Library里面的File才会支持在线编辑.直到今天早上我才发现用IE打开List里面的Attachments也是支持在线编辑的,但前提是必须是IE浏览器. 目前正 ...
- Java 8 flatMap example
Java 8 flatMap example In Java 8, Stream can hold different data types, for examples: Stream<Stri ...
- Java8 List字符串 去重
http://blog.csdn.net/jiaobuchong/article/details/54412094 public List<String> removeStringList ...
- springboot 项目单元测试
项目结构如下 1 引入测试的 maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...