【思路】

题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为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的更多相关文章

  1. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

    题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...

  2. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

  3. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  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 Many of us had played the game "Battle city" in our childhood, and some people ...

  6. C - Battle City BFS+优先队列

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  7. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  8. poj 2312 Battle City

    题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...

  9. Battle City

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Descr ...

随机推荐

  1. android隐藏EditText光标

    在android中如果有EditText,那么在载入时,光标会默认显示在第一个EditText框中,如果不想显示光标,且也不想把该光标移动到下一个EditText框,最简单的方法是在该 EditTex ...

  2. 配置连接的IP、端口、以及相应的数据库

    解压后里面有:lib 源文件 .examples 例子.test测试 将lib目录拷贝到你的项目中,就可以开始你的predis操作了. //使用autoload加载相关库,这边重点就是为了requir ...

  3. 跟踪内核启动过程CONFIG_DEBUG_LL【转自】

    转自:http://bbs.chinaunix.net/thread-3642079-1-1.html 最近在调试Linux内核,跟踪启动过程.发现在没有turn on mmu之前,可以使用物理地址, ...

  4. C# 数组 随机 排序

    ]; ; i < ; i++) { arrInt[i] = i; } arrInt = arrInt.OrderBy(c => Guid.NewGuid()).ToArray<int ...

  5. caffe Python API 之LRN

    net.mylrn = caffe.layers.LRN(net.pool1,local_size=5,alpha=1e-4,beta=0.75) 输出: layer { name: "my ...

  6. C#ActiveX控件开发

    1.新建项目,选择C#,选择.NET Framework2.0,新建一个Windows窗体控件库项目,命名为ActiveXDemo; 2.右击ActiveXDem项目,选择属性——应用程序——程序集信 ...

  7. vue的data用到this问题

    问题:在vue中用vue-awesome-swiper,在data中初始化,用到swiper一个方法onTap,然后再调vue的一个函数,用到this,可是... data() { return { ...

  8. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  9. 百度之星资格赛--IP聚合

    IP聚合 Accepts: 1901 Submissions: 4979 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/6553 ...

  10. ZOJ-3319

    Islands Time Limit: 1 Second      Memory Limit: 32768 KB There are N islands and some directed paths ...