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

思路:

遇见B就加2遇见E加1,,其他的不可以走。。

  1. #include<iostream#include<queue>
  1. #include<cstring>
  2. #include<cstdio>
  3. using namespace std;
  4. const int N=;
  5. char arr[N][N];
  6. int mark[N][N];
  7. int sa,ea,st,et;
  8. int n,m;
  9. struct stu{
  10. int x,y;
  11. int s;
  12. friend bool operator<(stu a,stu b){
  13. return a.s>b.s;//步数小的优先
  14. }
  15. }e1,e2;
  16. int base[][]={,,-,,,,,-};
    //4个方向
  17. void bfs(int x1,int y1,int x2,int y2){
  18. memset(mark,,sizeof(mark));
  19. priority_queue<stu> que;
  20. e1.x=x1,e1.y=y1,e1.s=;
  21. mark[x1][y1]=;
  22. que.push(e1);
  23. int ans=-;
  24. while(que.size()){
  25. e1=que.top();
  26. que.pop();
  27. if(e1.x==x2&&e1.y==y2) {
  28. ans=e1.s;
  29. break;
  30. }
  31. for(int i=;i<;i++){
  32. e2.x=e1.x+base[i][];
  33. e2.y=e1.y+base[i][if(e2.x<||e2.x>=n||e2.y<||e2.y>=m) continue;//判断越界
  34. if(mark[e2.x][e2.y] == ) continue;//判断是否走过
  35. if(arr[e2.x][e2.y]=='S'||arr[e2.x][e2.y]=='R') continue;//判断是否可以走
  36. if(arr[e2.x][e2.y]=='B') {//如果为B就加2
  37. e2.s=e1.s+;
  38. }
  39. else e2.s=e1.s+;
  40. que.push(e2);
  41. mark[e2.x][e2.y] = ;
  42. }
  43. }
  44. if(ans == -) puts("-1");
  45. else printf("%d\n", ans);
  46. }
  47. int main(){
  48. int x1,y1,x2,y2;
  49. while(cin>>n>>m){
  50. if(n==||m==)
  51. break;
  52. for(int i=;i<n;i++){
  53. scanf("%s",&arr[i]);
  54. }
  55. for(int i=;i<n;i++){
  56. for(int j=;j<m;j++){
  57. if(arr[i][j]=='Y'){
  58. x1=i;
  59. y1=j;
  60. }
  61. else if(arr[i][j]=='T'){
  62. x2=i;
  63. y2=j;
  64. }
  65. }
  66. }
  67. bfs(x1,y1,x2,y2);
  68. }
  69. return ;
  70. }
  1.  

C - Battle City BFS+优先队列的更多相关文章

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

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

  2. POJ - 2312 Battle City BFS+优先队列

    Battle City 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. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

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

  6. POJ 2312:Battle City(BFS)

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

  7. poj 2312 Battle City

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

  8. Battle City

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

  9. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

随机推荐

  1. 题解 SP2916 【GSS5 - Can you answer these queries V】

    前言 最近沉迷于数据结构,感觉数据结构很有意思. 正文 分析 先来分类讨论一下 1. \(x2<y1\) 如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \ ...

  2. MySQL对JSON类型UTF-8编码导致中文乱码探讨

    前言 继上文发表之后,结合评论意见并亲自验证最终发现是编码的问题,但是对于字符编码还是有点不解,于是乎,有了本文,我们来学习字符编码,在学习的过程中,我发现对于MySQL中JSON类型的编码导致数据中 ...

  3. Java导出Excel文件

    /** * 导出 用get请求 * @param response * @param * @throws IOException */ @RequestMapping(value = "/d ...

  4. 强化学习之七:Visualizing an Agent’s Thoughts and Actions

    本文是对Arthur Juliani在Medium平台发布的强化学习系列教程的个人中文翻译,该翻译是基于个人分享知识的目的进行的,欢迎交流!(This article is my personal t ...

  5. Building Applications with Force.com and VisualForce(Dev401)(十七):Data Management: Data management Tools

    ev401-018:Data Management: Data management ToolsModule Objectives1.List objects exposed in the impor ...

  6. OSLab:实模式与保护模式

    日期:2019/5/18 12:00 内容:操作系统实验作业:x86:IA-32:实模式与保护模式. PS:如果我们上的是同一门课,有借鉴代码的铁汁请留言告知嗷.只是作业笔记,不推荐学习. 一.实模式 ...

  7. oracle 10.1-10.4版本的oracle数据库要求

    1.针对arcgis 10.1的oracle数据库要求 受支持的数据库版本 标准版/标准独立版/企业版: Oracle 10g R2(64 位)10.2.0.3 Oracle 11g R1(64 位) ...

  8. ArrayList 扩容 和 Vector

    public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[siz ...

  9. 五、【Docker笔记】Dockers仓库

    仓库是集中存放镜像的地方,仓库的概念不要与注册服务器做混淆.注册服务器是存放仓库的具体服务器,每个服务器上可能有多个仓库,一个仓库有多个镜像. 仓库又可分为共有仓库和私有仓库,最大的共有仓库即Dock ...

  10. 1089 Insert or Merge (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...