【bfs+优先队列】POJ2312-Battle City
【思路】
题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了。第一次碰到时可能不能很好把握,第二次基本就可以当作水题了。
【错误点】
1.不能用裸的bfs。广搜的实际思想是将到达时间最短的放在队首,这样首次到达终点即为时间的最小值。通过砖墙的时间为两个单位,通过砖墙后可能不是时间最小值。用优先队列可以解决这一问题。
2.c++中memset初始化为memset(vis,0,sizeof(vis)),很多人可能会写成memset(vis,sizeof(vis),0)。
#include<iostream>
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int MAXN=+;
struct rec
{
int x,y,cost;
bool operator < (const rec &x) const
{
return (cost > x.cost);
}
};
char map[MAXN][MAXN];
int m,n,yx,yy; void init()
{
getchar();
for (int i=;i<m;i++)
{
for (int j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='Y')
{
yx=i;yy=j;
}
}
getchar();
}
} int bfs()
{
int vis[MAXN][MAXN];
int dx[]={,,,-};
int dy[]={,-,,};
priority_queue<rec> que;
memset(vis,,sizeof(vis));
vis[yx][yy]=;
rec now;
now.x=yx;now.y=yy;now.cost=;
que.push(now);
while (!que.empty())
{
rec head=que.top();
if (map[head.x][head.y]=='T') return(head.cost);
que.pop();
for (int i=;i<;i++)
{
int tempx=head.x+dx[i],tempy=head.y+dy[i];
if (tempx<||tempx>=m||tempy<||tempy>=n||map[tempx][tempy]=='R'||map[tempx][tempy]=='S'||vis[tempx][tempy]) continue;
vis[tempx][tempy]=;
now.x=tempx;now.y=tempy;
now.cost=head.cost+;
if (map[tempx][tempy]=='B') now.cost++;
que.push(now);
}
}
return(-);
} int main()
{
while (scanf("%d%d",&m,&n)!=EOF)
{
if (m==n && n==) break;
init();
cout<<bfs()<<endl;
}
return ;
}
【bfs+优先队列】POJ2312-Battle City的更多相关文章
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- 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 Many of us had played the game "Battle city" in our childhood, and some people ...
- C - 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 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- Battle City
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7208 Accepted: 2427 Descr ...
随机推荐
- 5.0docer 网络链接
docker0 :linux的虚拟网桥 虚拟网桥特点: 1.可以设置ip地址 2.相当于拥一个隐藏的虚拟网卡 安装网桥工具 apt-get install bridge-utils brctl ...
- 7.0docker镜像和仓库
repository:镜像的仓库 registry :docker组件的仓库,docker镜像的存储服务 tag :镜像的标签 例:ubuntu:14.04 ubuntu:latest 删除镜像 d ...
- python开发第二十六天CMDB
概要: 1.采集资产 2.API 一.资产采集 1.采集方式的配置 2.插件的定制 3.测试模式 4.错误日志(必须是行级的详细错误信息) 5.汇报数据-->遵循资产的唯一性 (1)只针对物理机 ...
- 自己动手实现arm函数栈帧回溯【转】
转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...
- python实战===短信验证码的小项目
项目地址 https://www.shiyanlou.com/courses/609/labs/2007/document flask的中文文档 http://docs.jinkan.org/docs ...
- linux内核启动分析(3)
主要分析do_basic_setup函数里面的do_initcalls()函数,这个函数用来调用所有编译内核的驱动模块中的初始化函数. static void __init do_initcalls( ...
- Oracle例外定义
例外名 ORA-XXXXX SQLCODE ACCESS_INTO_NULL ORA-06530 -6530 CASE_NOT_FOUND ORA-06592 -6592 COLLECTION_IS_ ...
- 在ubuntu上配置LAMP架构
1. 安装MySQL /* ubuntu默认进入系统是普通用户 所以在真实工作中,我们会得到root的授权. 所以我们需要用sudo做一切只有root才能完成的操作. */ [root@LAMP ~] ...
- 1.Python3标准库--前戏
Python有一个很大的优势便是在于其拥有丰富的第三方库,可以解决很多很多问题.其实Python的标准库也是非常丰富的,今后我将介绍一下Python的标准库. 这个教程使用的书籍就叫做<Pyth ...
- mybatis 一级缓存和二级缓存
1.默认是会话期内 一级session缓存 2.二级缓存: 引入二级缓存的jar, 配置 ehcache.xml, mapper.xml引入缓存<cache type="org.myb ...