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 ...
随机推荐
- string.Equals 比较2个字符串是否相同忽略大小写
bool res = string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase)
- Linq基本用法
- PHP获取APP客户端的IP地址的方法
分析php获取客户端ip 用php能获取客户端ip,这个大家都知道,代码如下: /** * 获取客户端ip * @param number $type * @return string */ func ...
- POJ3282+模拟
模拟题 /* 模拟 注意:相同一边的车有先后顺序! */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...
- window下编译ffmpeg 比较简单
网上关于编译ffmpeg的帖子很多,我也尝试了很多次,但是很多都过不了,一部分原因是版本问题,还有就是有的路劲没说的太明白导致的,经过一天的摸索,最终编译好了,下面把编译方式写下来,希望对看到帖子的人 ...
- Fundamental Datastructure
11988 - Broken Keyboard (a.k.a. Beiju Text) 可以用deque来模拟. #include <iostream> #include <stri ...
- 转XMLHelper
http://www.cnblogs.com/lixyvip/archive/2009/09/16/1567929.html using System; using System.Collection ...
- Servlet 下载文件
这几天有点懒散,还好没有忘记看书,上周去了国家图书馆翻阅了一些和Java相关的书籍,其实这些书都是自己以前看过或者听过,按理来说,不应该看自己已经看过的书籍,应该找一些最新的书籍去看,但是每次走到书架 ...
- hadoop之eclipse环境的配置
http://hi.baidu.com/kongxianghe123/item/ea352e1040cdeffd86ad4e28
- 【.Net免费公开课】--授技.Net中的高帅富技术-"工作流"
课程简介 免费公开课主题: .Net中的高帅富技术-“工作流” 公开课开课时间: 10月17日 19:30--21:30 公开课YY频道: 85155393 (重要:公开课QQ ...