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 ...
随机推荐
- textarea中限制输入字符长度(实用版)
textarea称文本域,又称文本区,即有滚动条的多行文本输入控件,在网页的提交表单中经常用到.与单行文本框text控件不同,它不能通过maxlength属性来限制字数,为此必须寻求其他方法来加以限制 ...
- [转]掌握 ASP.NET 之路:自定义实体类简介 --自定义实体类和DataSet的比较
转自: http://www.microsoft.com/china/msdn/library/webservices/asp.net/CustEntCls.mspx?mfr=true 发布日期 : ...
- struts2 标签的使用之二 s:iterator
struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:s:iterator 标签有3个属性: value:被迭代的集合 id :指定集 ...
- ecos内核概览--bakayi译
http://blog.csdn.net/wangzaiwei2006/article/details/6453423
- orm框架与缓存的关系
1.mybatis规定,一级缓存没必要bean类实现序列化,但二级缓存bean类必须实现序列化. 因为二级缓存是基于namespace的也就是基于接口的,二级缓存可以设置存储源,可以是redis或者m ...
- Eclipse中安装使用SVN
参考网址: Eclipse中使用SVN - 流逝的是岁月,沉淀的是经典 - 博客频道 - CSDN.NET http://blog.csdn.net/v123411739/article/detail ...
- 1400 - "Ray, Pass me the dishes!"
哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...
- 数据段、代码段、堆栈段、BSS段
在linux中,进程在内存中一般会分为5个段,用来存放从磁盘载入的程序代码,等. 这五个段分别是: BSS段: 通常用来存放程序中未初始化的全局变量的一块内存区域.属于静态内存分配. 问题:全局变量不 ...
- 对于eclipse新建maven工程需要注意的地方。
新建项目的时候,如果想配置默认的maven的jre为1.6或者别的. http://hi.baidu.com/hi_hi/item/765ec8bbc49880d384dd79d1 1.cmd命令建立 ...
- javascript里的post和get有什么区别
FORM中的get post方法区别Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法.二者主要区别如下: 1.Get是用来从服务器上获得数据,而Post ...