poj 2312 Battle City(优先队列+bfs)
题目链接: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)的更多相关文章
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- Poj(2312),坦克大战,BFS的变形
题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第 ...
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...
- POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
随机推荐
- Java Web前后端分离的思考与实践
第一节 Java Web开发方式的变化 Web开发虽然是我们常说的B/S模式,其实本质上也是一种特殊的C/S模式,只不过C和S的选择余地相对要窄了不少,而且更标准化.不论是采用什么浏览器和后端框架,W ...
- Wireshark lua dissector 对TCP消息包合并分析
应用程序发送的数据报都是流式的,IP不保证同一个一个应用数据包会被抓包后在同一个IP数据包中,因此对于使用自制dissector的时候需要考虑这种情况. Lua Dissector相关资料可以见:ht ...
- Android应用开发中的夜间模式实现(一)
前言 在应用开发中会经常遇到要求实现夜间模式或者主题切换具体例子如下,我会先讲解第一种方法. 夜间模式 知乎 网易新闻 沪江开心词场 Pocket 主题切换 腾讯QQ 新浪微博 我今天主要是详述第一种 ...
- hdu1506 Largest Rectangle in a Histogram
Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...
- BZOJ 3670 NOI2014 动物园 KMP+dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意概述:令num[i]表示字符串由1~i的字符形成的前缀中不相重叠的相同前后缀的数 ...
- mysql与hive2.1.1安装和配置
1.mysql安装 这个安装很简单,是在线安装,只需要按顺序执行一下几个命令就ok了. (1)sudo apt-get install mysql-server (2)sudo apt-get ins ...
- ScrollBarsEnabled的使用
在WinForm中通过WebBrowser获取网页,我想把WebBrowser的ScollBar去掉,我的网页不需要滚动条. 设置方法如下:单击WebBrowser设计页面,在属性页面有一个Scrol ...
- 2017南开ACM校赛(网络赛) 民间题解
orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...
- 【南开OJ2264】节操大师(贪心+二分+并查集/平衡树)
好久没更新了,今天就随便写一个吧 题目内容 MK和他的小伙伴们(共n人,且保证n为2的正整数幂)想要比试一下谁更有节操,于是他们组织了一场节操淘汰赛.他们的比赛规则简单而暴力:两人的节操正面相撞,碎的 ...
- [luogu2617]Dynamic Rankings
题面在这里 description 动态区间第\(k\)大 data range \[n,m\le 10000,a_i,t\le 1e^9\] solution 前置技能:主席树,静态区间第\(k\) ...