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. Java流中的map算子和flatMap算子的区别

    map算子和flatMap算子 map和flatMap都是映射(转换),那么他们之间究竟有什么区别呢? 1.我们先简单了解下map算子: @org.junit.Test public void tes ...

  2. ubuntu 16.04配置svn服务器

    为了操作方便,使用root登录服务器. 一.安装svn服务器 -->apt-get install subversion 二.创建svn版本库,存放需要管理内容路径 -->mkdir sv ...

  3. 常见排序算法总结分析之选择排序与归并排序-C#实现

    本篇文章对选择排序中的简单选择排序与堆排序,以及常用的归并排序做一个总结分析. 常见排序算法总结分析之交换排序与插入排序-C#实现是排序算法总结系列的首篇文章,包含了一些概念的介绍以及交换排序(冒泡与 ...

  4. 项目脚手架 - 《Spring Boot + MyBatis + MyBatis Generator》

    前言 最近启动了一个新的项目发现,每当一个新项目的启动往往需要从头搭建一个"框架",其中虽然很多基础代码可以Copy,但也会浪费不少时间. 基于这个情况,我打算在GitHub上创建 ...

  5. 深度学习框架Keras与Pytorch对比

    对于许多科学家.工程师和开发人员来说,TensorFlow是他们的第一个深度学习框架.TensorFlow 1.0于2017年2月发布,可以说,它对用户不太友好. 在过去的几年里,两个主要的深度学习库 ...

  6. 一个完整的机器学习项目在Python中的演练(二)

    大家往往会选择一本数据科学相关书籍或者完成一门在线课程来学习和掌握机器学习.但是,实际情况往往是,学完之后反而并不清楚这些技术怎样才能被用在实际的项目流程中.就像你的脑海中已经有了一块块"拼 ...

  7. 基于zookeeper实现分布式锁和基于redis实现分布所的区别

    1,实现方式不同 zookeeper实现分布式锁:通过创建一个临时节点,创建的成功节点的服务则抢占到分布式锁,可做业务逻辑.当业务逻辑完成,连接中断,节点消失,继续下一轮的锁的抢占. redis实现分 ...

  8. 软件架构的演进:单体、垂直、SOA、微服务

    软件架构演进 软件架构的发展经历了从单体结构.垂直架构.SOA架构到微服务架构的过程,以下为具体分类: 1.1.1      单体架构 特点: 1.所有的功能集成在一个项目工程中. 2.所有的功能打一 ...

  9. Failed RMAN Catalog Upgrade from 11.2.0.2 to 12.1.0.2 ( ORA-02296 RMAN-06004 )

    Failed RMAN Catalog Upgrade from 11.2.0.2 to 12.1.0.2  ( ORA-02296  RMAN-06004 ) 由于后期使用12c的数据库,需要对现有 ...

  10. 403 Invalid CORS request 跨域问题

    5.跨域问题 跨域:浏览器对于javascript的同源策略的限制 . 以下情况都属于跨域: 跨域原因说明 示例 域名不同 www.jd.com 与 www.taobao.com 域名相同,端口不同 ...