Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.

What we are discussing is a simple edition of this game.
Given a map that consists of empty spaces, rivers, steel walls and brick
walls only. Your task is to get a bonus as soon as possible suppose
that no enemies will disturb you (See the following picture).

Your tank can't move through rivers or walls, but it can
destroy brick walls by shooting. A brick wall will be turned into empty
spaces when you hit it, however, if your shot hit a steel wall, there
will be no damage to the wall. In each of your turns, you can choose to
move to a neighboring (4 directions, not 8) empty space, or shoot in one
of the four directions without a move. The shot will go ahead in that
direction, until it go out of the map or hit a wall. If the shot hits a
brick wall, the wall will disappear (i.e., in this turn). Well, given
the description of a map, the positions of your tank and the target, how
many turns will you take at least to arrive there?

Input

The input consists of several test cases. The first line of each
test case contains two integers M and N (2 <= M, N <= 300). Each
of the following M lines contains N uppercase letters, each of which is
one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R'
(river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test
case of M = N = 0 indicates the end of input, and should not be
processed.

Output

For each test case, please output the turns you take at least in a
separate line. If you can't arrive at the target, output "-1" instead.

Sample Input

3 4
YBEB
EERE
SSTE
0 0

Sample Output

8

这道题一开始就想到了优先队列,可就是不对,原来是优先队列建立的位置不对,建立在函数外,如果队列不空,会影响下组的结果,所以让他随着函数共存亡把

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
int n,m,sx,sy,tx,ty;
char map[][];
char vis[][];
int dir[][]={,,,,,-,-,};
struct que
{
int x,y,d;
friend bool operator <(que a,que b)
{
return a.d>b.d;
}
}temp,cn;
//priority_queue<que> q;///优先队列要建立在函数里 否则队列不空影响结果
int bfs(int x,int y)
{
priority_queue<que> q;
temp.x=x,temp.y=y,temp.d=,vis[sx][sy]=;
q.push(temp);
while(!q.empty())
{
cn=q.top();
q.pop();
for(int i=;i<;i++)
{
tx=cn.x+dir[i][];
ty=cn.y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty])continue;
vis[tx][ty]=;
int d=cn.d+(map[tx][ty]=='B'?:);
temp.x=tx,temp.y=ty,temp.d=d;
q.push(temp);
if(map[tx][ty]=='T')return d;
}
}
return -;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(!n&&!m)break;
memset(vis,,sizeof(vis));
sx=sy=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='Y')sx=i,sy=j;
else if(map[i][j]=='S'||map[i][j]=='R')vis[i][j]=;
}
}
cout<<bfs(sx,sy)<<endl;
}
}

Battle City 优先队列+bfs的更多相关文章

  1. poj 2312 Battle City(优先队列+bfs)

    题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...

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

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

  3. POJ 2312:Battle City(BFS)

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

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

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

  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

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

  8. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  9. Battle City

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

随机推荐

  1. [don't have permission to access]的一个经典原因

    那就是 ..... SELinux ...... 几年前好像经历过这个恶梦.现在又经历了一回. 从Windows上传了一个目录,做一个apache的别名Alias, 结果总是没有权限. chmod 7 ...

  2. English trip -- VC(情景课)1 A Get ready

    Meet your classmates 见见你的同学 Look at the picture. What do you see? 看图片.你看到了什么? calendar  日历 bookcase ...

  3. 20161210xlVBA一行数据转为四行

    Sub NextSeven_CodeFrame() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAlerts = Fal ...

  4. Confluence 6 配置 LDAP 连接池

    当 LDAP 连接池被启用后,LDAP 目录服务器将会维护一个连接池同时当必要的时候指派他们.当一个连接关闭后,这个连接将会放回到连接池中供以后进行使用.这种设置将会有效的提高系统性能. 希望配置 L ...

  5. Mashmokh and ACM CodeForces - 414D (贪心)

    大意: 给定n结点树, 有k桶水, p块钱, 初始可以任选不超过k个点(不能选根结点), 在每个点放一桶水, 然后开始游戏. 游戏每一轮开始时, 可以任选若干个节点关闭, 花费为关闭结点储存水的数量和 ...

  6. The Number Games CodeForces - 980E (树, 贪心)

    链接 大意: 给定$n$节点树, 求删除$k$个节点, 使得删除后还为树, 且剩余点$\sum{2^i}$尽量大 维护一个集合$S$, 每次尽量添加最大的点即可 这样的话需要支持求点到集合的最短距离, ...

  7. python-day33--Process类中的方法及属性

    p.daemon = True -->守护进程,守护进程不可以再有子进程,并且主进程死守护进程就死,要写在p.start()之前 p.join() ---> 主进程等子进程执行完 之后再结 ...

  8. zzuli2130卡时bfs题

    https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 2130: hipercijevi Time Limit: 1 Sec  Memory Li ...

  9. sql 智能提示

    依次打开SSMS—>工具—>选项—>文本编辑器—>Transact-SQL—>IntelliSense—>检查右侧窗体是否启用!!

  10. Syntax error: Bad for loop variable解决办法

    从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell:其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash. ...