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 ( ...
随机推荐
- 【IOI 2002/FJOI2019】任务安排(超级计算机)
题目 \(N\) 个任务排成一个序列在一台机器上等待完成(顺序不得改变),这 \(N\) 个任务被分成若干批,每批包含相邻的若干任务.从时刻 \(0\) 开始,这些任务被分批加工,第 \(i\) 个任 ...
- CentOS环境配置Hadoop(一)
配置Linux开发环境(hadoop-2.6.4) 一.准备工具 VMware-workstation-10.0.1注册机 CentOS-6.5-x86_64-bin-DVD1 jdk-7u79-li ...
- Gym101981I Magic Potion(最大流)
Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...
- css制作环形文本
css制作环形文本 在项目开发中,我们可能会遇到环形文本的需求,这个时候,怎样在代码以通俗易懂的前提下实现我们需要的效果呢?可能你会想到用一个一个的span元素计算出旋转的角度然后拼接起来,这个方案不 ...
- 【转载】Linux升级NTPD服务器-编译安装ntp-4.2.8p12与配置
[转载]Linux升级NTPD服务器-编译安装ntp-4.2.8p12与配置 1. 系统与软件版本 1.1 系统版本 rhel6.4(Red Hat Enterprise Linux Server r ...
- nginx安装-del
1.检测是否安装 rpm -q xxx2. 安装nginx前,我们首先要确保系统安装了g++.gcc.openssl-devel.pcre-devel和zlib-devel软件,可通过如图所示命令进行 ...
- 【EasyNetQ】- 快速开始
欢迎来到EasyNetQ.本指南向您展示如何在大约10分钟内启动并运行EasyNetQ. 你可以在GitHub上找到这个快速入门的代码:https://github.com/mikehadlow/Ea ...
- 关于网站转码(SiteApp转码)
1.Siteapp页面转码的意义?在百度移动搜索引擎中为更好满足用户信息需求,会同时为用户提供pc网页和mobile网页,但目前大多数PC页在移动终端中直接浏览的体验较差(交互.兼容和流量等).因此为 ...
- AGC017D Game on Tree(树型博弈)
题目大意: 给出一棵n个结点的树,以1为根,每次可以切掉除1外的任意一棵子树,最后不能切的话就为负,问是先手必胜还是后手必胜. 题解: 首先我们考虑利用SG函数解决这个问题 如果1结点有多个子节点,那 ...
- 2018牛客多校第一场 D.Two Graphs
题意: n个点,m1条边的图E1,n个点,m2条边的图E2.求图E2有多少子图跟图E1同构. 题解: 用STL的全排列函数next_permutation()枚举映射.对于每一种映射枚举每一条边判断合 ...