题目链接: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. JQuery UI 日历加时间

    写一个面试时间通知.用jquery ui 具体功能已经可以了,不过样式还没调 一.需要引入的文件,这些可以到官网下载 <link rel="stylesheet" href= ...

  2. 梳理 Opengl ES 3.0 (二)剖析一个GLSL程序

    OpenGL ES shading language 3.0 也被称作 GLSL,是个 C风格的编程语言. Opengl ES 3.0内部有两种可编程处理单元,即Vertex processor和Fr ...

  3. POJ 2229 递推

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  4. 九度OJ--Q1163

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 输入一个整数n(2<=n<=10000),要求输出所有从 ...

  5. python xlrd处理表格常用方法

    1.导入模块import xlrd2.打开Excel文件读取数据data = xlrd.open_workbook('excelFile.xls')3.使用技巧获取一个工作表 table = data ...

  6. android-ViewList的通用ViewHold

    在写ViewList的时候要写Adapter的时候,经常大量的代码都是差不多的. 1 ViewHold 2 if(convertView ==null ){}else{} 3 setTag 4 FIn ...

  7. Java FTP下载文件以及编码问题小结

    问题 之前在开发过程中,遇到了一点问题,我要访问一个FTP服务器去下载文件详细情况如下: 1. 需要传入一个可能为中文的文件名: 2. 通过文件名去FTP上寻找该文件: 3. FTP服务器的命名编码为 ...

  8. systemtap如何写C函数 捎带着看看ret kprobe怎么用

    在systemstap中自定义函数 Embedded C can be the body of a script function. Instead enclosing the function bo ...

  9. rpc通信模型

    1.client_stub是为了屏蔽客户端调用远程主机的对象,而在本地的一个对象存根,存根负责接受本地方法调用,并将其序列化,然后通过网络发送给服务端.

  10. delphi Edit 控制最大值,只能输入数字型 控制小数位数

    delphi语言受众多程序员追捧,主要原因之一就是它有很多第三方的控件可供使用.很多资深的delphi程序员都把自己积累的函数.过程等设计成控件,以方便使用,提高开发效率. 本文通过一个只允许输入数字 ...