中南大学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) ...
随机推荐
- linux中DHCP服务配置文件/etc/dhcpd.conf详细说明
DHCP服务的配置 dhcpd.conf 是DHCP服务的配置文件,DHCP服务所有参数都是通过修改dhcpd.conf 文件来实现,安装后dhcpd.conf 是没有做任何配置的,将/usr/sha ...
- HDU 4576 Robot (概率 & 期望)
Robot Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Sub ...
- React(0.13) 组件的组合使用
<html> <head> <title>组件的组合调用</title> <script src="build_0.13/react.m ...
- Redis 启动警告错误解决[转]
Redis 启动警告错误解决 启动错误 1.WARNING overcommit_memory is set to 0! Background save may fail under low memo ...
- 【C语言】字符串常量与指针
- Java – How to join Arrays
Java – How to join Arrays In this article, we will show you a few ways to join a Java Array. Apache ...
- BIO NIO AIO 简介
原文: https://github.com/zhongmingmao/nio_demo 简介 NIO与AIO的简单使用 基本概念 同步与异步 同步和异步是针对应用程序和内核的交互而言的:同步指的是用 ...
- 跨系统-跨数据库访问表或创建映射视图-PB访问数据库的特殊处理
一. 在项目中可能会遇到在一个项目中会出现多个数据库的操作. 二. 也可能会多个系统多个数据库之间进行交互操作显示. 三. 经常需要两个数据库关联查询或需要翻译显示内容时会很麻烦. 四. 今天就有个简 ...
- RxJava学习笔记(组合Observables)(转)
组合Observable 当我们需要处理多个Observable的时候,我们可以通过Rxjava的组合函数来创建我们自己想要的Observable. Merge RxJava的 merge() 方法将 ...
- 【小超_Android】2015最流行的android组件、工具、框架大全(兴许)
2015.07.07 FlyRefresh 创意Replace的Android实现,非常cool. fab-toolbar Material Design风格的FAB工具栏效果 MaterialVie ...