题目链接:http://poj.org/problem?id=2312

题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟。最后求解Y--->T的最短时间!!

看到这题首先想到广搜来找最短时间,但是这里可以对B和E进行处理,方便计算~

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dir[][]= {,,-,,,,,-};
bool vis[][];
char map[][];
int m,n,sx,sy;
struct node
{
int x,y,time;
friend bool operator<(node a,node b)
{
return a.time>b.time;
}
}; int bfs()
{
node s,ss,sss;
priority_queue<node>q;
s.x=sx;
s.y=sy;
s.time=;
q.push(s);
vis[sx][sy]=;
while (!q.empty())
{
ss=q.top();
q.pop();
for (int i=; i<; i++)
{
sss.x=ss.x+dir[i][];
sss.y=ss.y+dir[i][];
if (sss.x<||sss.y<||sss.x>=n||sss.y>=m||map[sss.x][sss.y]=='R' || map[sss.x][sss.y]=='S'||vis[sss.x][sss.y])
continue;
if (map[sss.x][sss.y]=='B')
sss.time=ss.time+;
else
sss.time=ss.time+;
//sss.time=ss.time+1;
if (map[sss.x][sss.y]=='T')
return sss.time;
vis[sss.x][sss.y]=;
q.push(sss);
}
}
return -;
} int main ()
{
while (~scanf("%d%d",&n,&m))
{
if (n==&&m==)
break;
memset(vis,,sizeof(vis));
for (int i=; i<n; i++)
{
getchar();
for (int j=; j<m; j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='Y')
{
sx=i;
sy=j;
}
}
}
printf ("%d\n",bfs());
}
return ;
}

还有一种,单纯的广搜也是可以的。

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dir[][]= {,,-,,,,,-};
bool vis[][];
char map[][];
int m,n,sx,sy;
struct node
{
int x,y,time;
}; int bfs()
{
node s,ss,sss;
//priority_queue<node>q;
queue<node>q;
s.x=sx;
s.y=sy;
s.time=;
q.push(s);
vis[sx][sy]=;
while (!q.empty())
{
ss=q.front();
q.pop();
for (int i=; i<; i++)
{
sss.x=ss.x+dir[i][];
sss.y=ss.y+dir[i][];
if (sss.x<||sss.y<||sss.x>=n||sss.y>=m||map[sss.x][sss.y]=='R' || map[sss.x][sss.y]=='S'||vis[sss.x][sss.y])
continue;
if (map[sss.x][sss.y]=='B')
sss.time=ss.time+;
else
sss.time=ss.time+;
//sss.time=ss.time+1;
if (map[sss.x][sss.y]=='T')
return sss.time;
vis[sss.x][sss.y]=;
q.push(sss);
}
}
return -;
} int main ()
{
while (~scanf("%d%d",&n,&m))
{
if (n==&&m==)
break;
memset(vis,,sizeof(vis));
for (int i=; i<n; i++)
{
getchar();
for (int j=; j<m; j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='Y')
{
sx=i;
sy=j;
}
}
}
printf ("%d\n",bfs());
}
return ;
}

poj 2312 Battle City(优先队列+bfs)的更多相关文章

  1. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  2. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  3. poj 2312 Battle City

    题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...

  4. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  5. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  6. Poj(2312),坦克大战,BFS的变形

    题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第 ...

  7. poj 2049 Finding Nemo(优先队列+bfs)

    题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...

  8. POJ 2435Navigating the City(bfs)

    题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...

  9. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

随机推荐

  1. jmeter上传视频图片附件

    http上传附件一般用的Content-Type: multipart/form-data;文中是先通过fiddler抓取手机端的请求,然后通过jmeter模拟该请求,如果有接口文档,则可以跳过抓包这 ...

  2. (2)分布式下的爬虫Scrapy应该如何做-关于对Scrapy的反思和核心对象的介绍

    本篇主要介绍对于一个爬虫框架的思考和,核心部件的介绍,以及常规的思考方法: 一,猜想 我们说的爬虫,一般至少要包含几个基本要素: 1.请求发送对象(sender,对于request的封装,防止被封) ...

  3. Prim求MST最小生成树

    最小生成树即在一个图中用最小权值的边将所有点连接起来.prim算法求MST其实它的主要思路和dijkstra的松弛操作十分相似 prim算法思想:在图中随便找一个点开始这里我们假定起点为“1”,以点1 ...

  4. Go基础篇【第2篇】: 内置库模块 fmt

    fmt官方文档说明:https://studygolang.com/pkgdoc import "fmt" mt包实现了类似C语言printf和scanf的格式化I/O.格式化动作 ...

  5. coredump分析

    首先通过命令 gdb freeswitch core.60954进入gdb. 这里freeswitch 是产生coredump的可执行应用,core.60954是应用产生的coredump文件. 然后 ...

  6. 怎么获取textarea中选中文字

    textarea设置select="saveSelectionText()" //保存选中内容 saveSelectionText: function () { var focus ...

  7. HL7 2.6 解析(XML)

    用途:检验化验(LIS)实验室设备数据交换解析. using System; using System.Collections.Generic; using System.Text; using Sy ...

  8. CSS3基础选择器

    /*选择器分组:多个选择器使用同一个样式*/ h1,h2,a{ color: blue; } strong{ color: aquamarine; } /*选择器继承:body中未设置样式的会使用继承 ...

  9. BZOJ4416 SHOI2013阶乘字符串(状压dp)

    当n大到一定程度(>21)时一定无解,并不会证. 如果要取出一个排列,显然应该让每一位在序列中的位置尽量靠前.于是设f[S]表示存在S子集中这些字母所组成的所有排列的最短前缀的长度,枚举当前排列 ...

  10. VB托盘图标不响应WM_MOUSEMOVE的原因及解决方法

    文章参考地址:http://blog.csdn.net/txh0001/article/details/38265895:http://bbs.csdn.net/topics/330106030 网上 ...