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

  1. 3 4
  2. YBEB
  3. EERE
  4. SSTE
  5. 0 0

Sample Output

  1. 8
  2.  
  3. 这道题一开始就想到了优先队列,可就是不对,原来是优先队列建立的位置不对,建立在函数外,如果队列不空,会影响下组的结果,所以让他随着函数共存亡把
  4.  
  5. 代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <queue>
  6. #include <string>
  7. #include <cmath>
  8. using namespace std;
  9. int n,m,sx,sy,tx,ty;
  10. char map[][];
  11. char vis[][];
  12. int dir[][]={,,,,,-,-,};
  13. struct que
  14. {
  15. int x,y,d;
  16. friend bool operator <(que a,que b)
  17. {
  18. return a.d>b.d;
  19. }
  20. }temp,cn;
  21. //priority_queue<que> q;///优先队列要建立在函数里 否则队列不空影响结果
  22. int bfs(int x,int y)
  23. {
  24. priority_queue<que> q;
  25. temp.x=x,temp.y=y,temp.d=,vis[sx][sy]=;
  26. q.push(temp);
  27. while(!q.empty())
  28. {
  29. cn=q.top();
  30. q.pop();
  31. for(int i=;i<;i++)
  32. {
  33. tx=cn.x+dir[i][];
  34. ty=cn.y+dir[i][];
  35. if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty])continue;
  36. vis[tx][ty]=;
  37. int d=cn.d+(map[tx][ty]=='B'?:);
  38. temp.x=tx,temp.y=ty,temp.d=d;
  39. q.push(temp);
  40. if(map[tx][ty]=='T')return d;
  41. }
  42. }
  43. return -;
  44. }
  45. int main()
  46. {
  47. while(scanf("%d%d",&n,&m))
  48. {
  49. if(!n&&!m)break;
  50. memset(vis,,sizeof(vis));
  51. sx=sy=;
  52. for(int i=;i<n;i++)
  53. {
  54. for(int j=;j<m;j++)
  55. {
  56. cin>>map[i][j];
  57. if(map[i][j]=='Y')sx=i,sy=j;
  58. else if(map[i][j]=='S'||map[i][j]=='R')vis[i][j]=;
  59. }
  60. }
  61. cout<<bfs(sx,sy)<<endl;
  62. }
  63. }

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. cmd 常用操作

    2017-08-24 16:05:28 cd : change directory(目录) 功能:显示当前目录 类型:内部命令 格式:CD[盘符:][路径名][子目录名] 说明:CD命令不能改变当前所 ...

  2. 女生学java是否真的没有优势

    随着女性越来越独立,我们可以看到再以前我们认为不适合女性朋友从事的工作,也出现了越来越多的女生,例如对IT行业也不再跟之前一样畏惧.虽然当下很多人所持的观点依旧是,女生不适合IT行业,但是很多女生已经 ...

  3. Illumina Sequence Identifiers 序列标识符 index详解

    大家基本都知道什么是 FASTA 和 FastQ 格式了,但这是不够的. 我们还需要了解世界上最大的测序公司自己定制的 FastQ 格式,因为你可能会经常用到,有时还会亲自去处理它们. 本文主题:Il ...

  4. 探索Bioconductor数据包

    参考: R的bioconductor包TxDb.Hsapiens.UCSC.hg19.knownGene详解 Bioconductor的数据包library(org.Hs.eg.db)简介

  5. English trip -- VC(情景课)1 B Countries

    Vocabulary focus 核心词汇 Vo ca bu la ry   fo cus [və(ʊ)'kæbjʊlərɪ]      ['fəʊkəs] Listen and repeat  听并 ...

  6. spoj Help the Military Recruitment Office!

    题意:给出名字和地方,地方会重定向,最后再给出名字,问现在属于哪里? 用并查集. //#pragma comment(linker,"/STACK:1024000000,1024000000 ...

  7. SQL Server数据库入门学习总结

    数据库基本是由表,关系,操作组成:对于初学者,首先要学的是: 1.数据库是如何存储数据的 —— 表.约束.触发器 2.数据库是如何操作数据的 —— insert,update,delete.T-sql ...

  8. angularjs 中的scope继承关系——(1)

    转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html JavaScript 的原型链继承 假设父类 parentScop ...

  9. Linq的简介和基础知识学习

    学习LINQ之前,我们要知道LINQ是干什么,解决什么问题的,怎样学习? 一.LINQ简介 1.什么是LINQ? 什么是LINQ?LINQ中文翻译为语言集成查询(Language Integrated ...

  10. BZOJ1197 [HNOI2006]花仙子的魔法

    其实是一道奇怪的DP题,蒟蒻又不会做... 看了Vfk的题解才算弄明白是怎么一回事: 令f[i, j]表示i维有j个球时最大切割部分,则 f[i, j] = f[i, j - 1] + f[i - 1 ...