【思路】

题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为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. 小程序_RSA加密功能

    这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...

  2. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  3. linux编程之信号

    信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...

  4. python mysql插入数据遇到的错误

    1.数据插入的时候报错:not enough arguments for format string,大概意思就是说没有足够的参数格式化字符串. 我的数据库插入方法是这样的 def add_data( ...

  5. 设计模式之笔记--工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 定义 工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 类图 描 ...

  6. 深度解析Python动态语言

    1.动态语言的定义 动态编程语言是高级程序设计语言的一个类别,在计算机科学领域已被广泛应用.它是一类在运行时可以改变其结构的语言:例如新的函数.对象.甚至代码可以被引进,已有的函数可以被删除或是其他结 ...

  7. 零基础学php的自学

    我们都知道,php语言作为一种专业建站的语言,没有华而不实,而是经受住了时间考验,成为一种值得学习的语言.现在国内众多的php学校也说明,php语言在当今有着广泛的市场需求. 那么零基础的同学如何学习 ...

  8. Redis、mongdb、memcached的个人总结

    有测试的实例:http://colbybobo.iteye.com/blog/1986786 详细描述优缺点:https://www.cnblogs.com/binyue/p/4582550.html

  9. Leetcode 之Balanced Binary Tree(49)

    用递归的方式来做,左右两棵子树的高度差不超过1.分成两部分,一部分递归得到树的高度,一部分递归检查左右子树是否是平衡二叉树. int getHeight(TreeNode *root) { ; ; } ...

  10. Leetcode 之Length of Last Word(37)

    扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...