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

思路:

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

#include<iostream#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=;
char arr[N][N];
int mark[N][N];
int sa,ea,st,et;
int n,m;
struct stu{
int x,y;
int s;
friend bool operator<(stu a,stu b){
return a.s>b.s;//步数小的优先
}
}e1,e2;
int base[][]={,,-,,,,,-};
//4个方向
void bfs(int x1,int y1,int x2,int y2){
memset(mark,,sizeof(mark));
priority_queue<stu> que;
e1.x=x1,e1.y=y1,e1.s=;
mark[x1][y1]=;
que.push(e1);
int ans=-;
while(que.size()){
e1=que.top();
que.pop();
if(e1.x==x2&&e1.y==y2) {
ans=e1.s;
break;
}
for(int i=;i<;i++){
e2.x=e1.x+base[i][];
e2.y=e1.y+base[i][
if(e2.x<||e2.x>=n||e2.y<||e2.y>=m) continue;//判断越界
if(mark[e2.x][e2.y] == ) continue;//判断是否走过
if(arr[e2.x][e2.y]=='S'||arr[e2.x][e2.y]=='R') continue;//判断是否可以走
if(arr[e2.x][e2.y]=='B') {//如果为B就加2
e2.s=e1.s+;
}
else e2.s=e1.s+;
que.push(e2);
mark[e2.x][e2.y] = ;
}
}
if(ans == -) puts("-1");
else printf("%d\n", ans);
} int main(){
int x1,y1,x2,y2;
while(cin>>n>>m){
if(n==||m==)
break;
for(int i=;i<n;i++){
scanf("%s",&arr[i]);
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(arr[i][j]=='Y'){
x1=i;
y1=j;
}
else if(arr[i][j]=='T'){
x2=i;
y2=j;
}
}
}
bfs(x1,y1,x2,y2);
}
return ;
}
												

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. MySQL----DDL(操作数据库,表)

    1. 操作数据库:CRUD 1. C(Create):创建 * 创建数据库: * create database 数据库名称; * 创建数据库,判断不存在,再创建: * create database ...

  2. 【2019HDU多校】第九场1006/HDU6685-Rikka with Coin——位运算打表

    题目链接 题目大意 使用10.20.50.100元面额的硬币能分别组成题目给出的面额,需要最少的硬币个数 分析 一开始队友想用一堆if-else解决问题,然后WA了无数发-- 我想到了一种比较简单的打 ...

  3. CMD 基础命令

    基本命令 1.编译.java文件成.class:找到文件所在路径 --> java -d . 文件名称.java --> javac -d . 文件名称.java : 2.ping URL ...

  4. Java基础语法(7)-数组

    title: Java基础语法(7)-数组 blog: CSDN data: Java学习路线及视频 1.数组的概述 数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并 ...

  5. MQ的理论理解

    MQ(消息队列)简介 概念 : 消息队列(MQ)是一种应用程序对应用程序的通信方法. 应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们. 消息传递指的是程序之间 ...

  6. Python第六章-函数05-迭代器&生成器

    python作为一个既面向对象,又支持函数式编程的语言,函数的使用方面有很多特点. 比如:闭包,装饰器,迭代器等 函数的高级应用 容器:生活中常见的容器有哪些?袋子,盆子,水杯,书包,铅笔盒... 容 ...

  7. 4D

    GIS行业通常将GIS常用的数据产品概括为“4D”,即:DOM(数字正射影像图).DEM(数字高程模型).DLG(数字线划地图).DRG(数字栅格地图).以及复合模式派生数据组成. DOM:数字正射影 ...

  8. SimpleITK中术语

    在SimpleITK中,各术语对应如下: Width: 宽度,X轴,矢状面Height: 高度,Y轴,冠状面Depth: 深度, Z轴,横断面 引用自:https://blog.csdn.net/Ji ...

  9. 读者来信 | 刚搭完HBase集群,Phoenix一启动,HBase就全崩了,是什么原因?(已解决)

    前言:之前有朋友加好友与我探讨一些问题,我觉得这些问题倒挺有价值的:于是就想在本公众号开设一个问答专栏,方便技术交流与分享,专栏名就定为:<读者来信>.如遇到本人能力有限难以解决的问题,我 ...

  10. Java Web项目bug经验202002112049

    运行程序后,如果配置有问题,可能不会进代码,而直接报错.