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. Django 模型笔记

    关于模型: 1:一个模型类对应一个表,模型类中的属性对应表中的一个字段 2:字段类型(数据库支持的类型) 模型属性 字符串 1:CharField(Maxlength=长度) models.CharF ...

  2. windows10删除用户头像

    点击开始菜单,然后这里我们点击最上方的用户,弹出的界面,点击这里的更改帐户设置,大家如图进行操作,点击这里即可.   这里我们通过浏览可以修改自己的账户头像,问题是怎么删除这里使用过的账户头像呢?这里 ...

  3. vue基础指令学习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 卷积的发展历程,原理和基于 TensorFlow 的实现

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 稀疏交互 在生物学家休博尔和维瑟尔早期关于猫视觉皮层的研究中发现, ...

  5. 俩个对象的hashCode()相同,则equals()也一定为true,对吗?

    不对,俩个对象的hashCode()相同,equals()不一定为true. 代码示例: 1 String str1 = "通话"; 2 String str2 = "重 ...

  6. 面试刷题31:分布式ID设计方案

    面试中关于分布式的问题很多.(分布式事务,基本理论CAP,BASE,分布式锁)先来一个简单的. 简单说一下分布式ID的设计方案? 首先要明确在分布式环境下,分布式id的基本要求. 1, 全局唯一,在分 ...

  7. 操作系统-IO与显示器

    1. 让外设工作起来 只要给相应的控制器中的寄存器发一个指令 向设备控制器的寄存器写不就可以了吗? 需要查寄存器地址.内容的格式和语义.操作系统需要给用户提供一个简单视图---文件视图,这样方便 总的 ...

  8. lly的瞬移方块(并查集)

    lly的瞬移方块 Description llyllylly最近发明了一个叫瞬移方块的游戏,为啥llyllylly这么闲呢,这得从一只蝙蝠说起..... llyllylly决定给大家也分享一下这个游戏 ...

  9. 从 Linux 操作系统谈谈 IO 模型(终)

    Linux 为什么要区分内核空间与用户空间? Linux 操作系统的 IO 模型有哪几种?有啥区别? 常说的阻塞现象,到底是咋回事? 网络编程研发时,那块到底耗时最多,代码是否还有优化空间? 前几期的 ...

  10. idea运行javadoc生成文档以及 报错编码gbk的不可映射字符坑

    将项目类信息生成文档 idea整合了javadoc的操作,可以一键生成doc文档 方法: 选中你要生成文档的项目 点击上方tools->Generate JavaDoc 运行即可 注意这里有一个 ...