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 ( ...
随机推荐
- Linux 文件属性及修改权限
输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...
- 【Swift】日期比较函数 记录下 Comparing date in Swift
Add this code to your project and comparing dates is easier than ever 扩展NSDATE //swift 3.0.2 extensi ...
- 梳理 Opengl ES 3.0 (五)shader运行原理
先来看看一张图 shader都是在运行时编译和执行的,每个shader都有一个main函数作为它的入口. vertex shader的功能有两个:一个是计算顶点坐标变换,另一个就是为片元shader计 ...
- 【leetcode】19. 删除链表的倒数第N个节点
描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变 ...
- kubernetes(k8s) 集群
开启和重启命令: sudo systemctl stop etcdsudo systemctl stop dockersudo systemctl stop kube-apiserversudo sy ...
- LeetCode 707 ——设计链表
1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...
- sendto函数的坑
测试unix数据报套接字时,一个程序收,一个程序发,分别绑定自己的socket.结果在收的部分,返回的发送方的地址总是空的,但是返回的地址长度又是对的. ) { bzero(&clientad ...
- Python数据分析(三)pandas resample 重采样
下方是pandas中resample方法的定义,帮助文档http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling中有 ...
- zTree基本功能[core]
zTree 是一个依靠jQuery实现的多功能"树插件".优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree v3.0 将核心代码按照功能进行了分割,不需 ...
- P3444 [POI2006]ORK-Ploughing
题目描述 Byteasar, the farmer, wants to plough his rectangular field. He can begin with ploughing a slic ...