题意:从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(广搜+优先级队列)的更多相关文章

  1. 『ice 离散化广搜』

    ice(USACO) Description Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000. 湖面上的N(1 & ...

  2. hdu 1253 胜利大逃亡(广搜,队列,三维,简单)

    题目 原来光搜是用队列的,深搜才用栈,我好白痴啊,居然搞错了 三维的基础的广搜题 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #in ...

  3. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  4. c++ 优先级队列(priority_queue)

    从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码.实在郁闷,于是自己在此归纳归纳. 废话不多说,直入主题. 优先级队列的核心是比较函数的实现. 比较函数有两种实现方法: 1.在 ...

  5. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  6. 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 ...

  7. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  8. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  9. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

随机推荐

  1. select count(*)和select count(1)的区别 (转)

    A 一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键 ...

  2. 有关js的变量、作用域和内存问题

    来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(四) js共有5种基本数据类型:Undefined.NULL.Boolean.Numbe ...

  3. [Firefly引擎][学习笔记一][已完结]带用户验证的聊天室

    原地址:http://bbs.9miao.com/thread-44571-1-1.html 前言:早在群里看到大鸡蛋分享他们团队的Firefly引擎,但一直没有时间去仔细看看,恰好最近需要开发一个棋 ...

  4. python List&Set&Dict交集、并集、差集

    1.python List交集.并集.差集 1). 获取两个list 的交集#方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in  ...

  5. URAL 1353 Milliard Vasya's Function(DP)

    题目链接 题意 : 让你找出1到10^9中和为s的数有多少个. 思路 : 自己没想出来,看的题解,学长的题解报告 题解报告 //URAL 1353 #include <iostream> ...

  6. Web Api 入门

    http://www.cnblogs.com/developersupport/p/WebAPI-Security.html http://www.cnblogs.com/r01cn/archive/ ...

  7. Ubuntu Server 中resolv.conf重启时被覆盖的问题

    /etc/resolv.conf中设置dns之后每次重启Ubuntu Server时该文件会被覆盖,针对这种情况找了一些个解决方法 防止/etc/resolv.conf被覆盖的方法 方法一 1.需要创 ...

  8. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  9. 创业公司Playcafe关门大吉 创始人总结10大失败教训

    导读:互联网电视游戏网站PlayCafe的创始人马克·高登森(Mark Goldenson)日前撰文,总结了自己创业失败的十个教训.以下为文章主要内容: 一年半前,我与公司联合创始人戴维·奈格(Dev ...

  10. 200. Number of Islands

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...