VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
题意:从s到m的最短时间。(“o"不能走,‘#’走一个花两个单位时间,‘.'走一个花一个单位时间)
思路:广搜和优先队列。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define MAX 30
using namespace std; struct pos
{
int x;
int y;
int step;
}; bool operator<(const pos &a, const pos &b)
{
return a.step > b.step;
} pos sp, ep;
char map[MAX][MAX];
int dir[][] = {{, }, {-, }, {, }, {, -}}, m, n, ti; int bfs()
{
priority_queue<pos> q;
pos temp, t;
temp = sp;
temp.step = ;
q.push(temp);
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.step >= ti)
{
continue;
}
if(temp.x == ep.x && temp.y == ep.y && temp.step < ti)
{
return temp.step;
}
for(int i = ; i < ; i++)
{
t.x = temp.x + dir[i][];
t.y = temp.y + dir[i][];
if(t.x >= && t.x < n && t.y >= && t.y < m && map[t.x][t.y] != 'o')
{
if(map[t.x][t.y] == '.')
{
t.step = temp.step + ;
map[t.x][t.y] = 'o';
q.push(t);
}
else if(map[t.x][t.y] == '#')
{
t.step = temp.step + ;
map[t.x][t.y] = 'o';
q.push(t);
}
else if(map[t.x][t.y] == 'm')
{
t.step = temp.step + ;
map[t.x][t.y] = 'o';
q.push(t);
}
}
}
}
return -;
} int main()
{
scanf("%d%d%d", &ti, &m, &n);
int ans;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
cin>>map[i][j];
if(map[i][j] == 's')
{
sp.x = i;
sp.y = j;
}
if(map[i][j] == 'm')
{
ep.x = i;
ep.y = j;
}
}
}
ans = bfs();
if(ans == -)
{
printf("55555\n");
}
else
{
printf("%d\n", ans);
}
return ;
}
VIJOS-P1340 拯救ice-cream(广搜+优先级队列)的更多相关文章
- 『ice 离散化广搜』
ice(USACO) Description Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000. 湖面上的N(1 & ...
- hdu 1253 胜利大逃亡(广搜,队列,三维,简单)
题目 原来光搜是用队列的,深搜才用栈,我好白痴啊,居然搞错了 三维的基础的广搜题 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #in ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- c++ 优先级队列(priority_queue)
从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码.实在郁闷,于是自己在此归纳归纳. 废话不多说,直入主题. 优先级队列的核心是比较函数的实现. 比较函数有两种实现方法: 1.在 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
随机推荐
- smarty 时间格式化date_format
代码如下:$smarty = new Smarty; $smarty->assign('yesterday', strtotime('-1 day')); $smarty->display ...
- Ubuntu环境下手动配置openSSH
配置openSSH 1.手动下载压缩文件(.tar.gz) zlib-1.2.7.tar.gz openssl-1.0.1j.tar.gz openssh-6.0p1.tar.gz 2.安装zlib ...
- loadrunner java协议脚本要点
常见问题 1. Error: Thread Context: Call to service of the driver failed, reason - thread context wasn't ...
- 李洪强漫谈iOS开发[C语言-012]-C语言基本数据类型
// // main.m // 08 - 基本数据类型 // // Created by vic fan on 16/7/16. // Copyright © 2016年 李洪强. All r ...
- 初探 ref 和 out
在一面试题中曾遇到这一题目: 说出下面几个函数的区别: private void test(string str){…} private void test(ref string str){…} pr ...
- iphone6S“玫瑰金”的秘密——阳极氧化
阳极氧化对多数人来说是一个熟悉又陌生的名词,大多数可能知道它的作用之一就是是能使金属呈现各种各样色彩.最为人熟知的运用阳极氧化技术的产品就是iphone系列产品了,已经推出了金色,玫瑰金色,深空灰色, ...
- SQL盲注修订建议
一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...
- Android性能优化典范 - 第2季
Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...
- Linux命令面试常考的简单汇总
1.显示日期与时间的命令:date 2.显示日历的命令:cal 3.简单好用的计算器:bc 4.热键“命令补全或文件补齐”:Tab 5.热键“中断目前程序”:Ctrl+C 6.热键“键盘输入结束(En ...
- [转]vim ruby等的ide设置
使用vim做rails开发,推荐这个 https://github.com/carlhuda/janus 1. vim下的Rails常用插件 首先列出我比较常用的vim插件,基本都是网上提到的哪些.必 ...