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. ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory

    问题描述: ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory 首先检 ...

  2. Ubuntu18.04下安装MySQL5.7(支持win10-wsl环境)

    注意: 本文操作环境为win10系统wsl下的Ubuntu18.04,对于原生的Ubuntu18.04同样适用.MySQL默认版本为5.7,其他版本不适用. 安装步骤 1.更新源: sudo apt ...

  3. TensorFlow v2.0实现Word2Vec算法

    使用TensorFlow v2.0实现Word2Vec算法计算单词的向量表示,这个例子是使用一小部分维基百科文章来训练的. 更多信息请查看论文: Mikolov, Tomas et al. " ...

  4. macro

    Hello, 宏定义魔法世界 宏只是在预处理器里进行文本替换,没有类型,不做任何类型检查,编译器可以对相同的字符串进行优化.只保存一份到 .rodata 段.甚至有相同后缀的字符串也可以优化,你可以用 ...

  5. [vijos1844]遭遇战<SPFA>

    题目链接:https://vijos.org/p/1404 我在二分查找区找到这道题的,没错这就是一道摆在二分区但是可以用SPFA做的一道题(当然我后来去看了下分区,貌似这题的方法很多) 这一题还是比 ...

  6. Java复合优先于继承

    复合优于继承 继承打破了封装性(子类依赖父类中特定功能的实现细节) 合理的使用继承的情况: 在包内使用 父类专门为继承为设计,并且有很好的文档说明,存在is-a关系 只有当子类真正是父类的子类型时,才 ...

  7. vue实现选项卡切换--不用ui库

    vue的ui库中基本都有选项卡切换的组件,但是在项目开发过程中却不一定能很好的为我们所用,因为里面的样式和 一些状态并不能很好的根据我们的项目需求进行定制.最近项目中使用的是vant-ui中的标签页, ...

  8. Light of future-冲刺Day 2

    目录 归属班级 →2019秋福大软件工程实践Z班 作业要求 →团队作业第五次-项目冲刺 团队名称 未来之光 这个作业的目标 第二天的冲刺总结 作业正文 →Light of future-冲刺Day 2 ...

  9. fiddler设置重定向

    fiddler支持将指定模式的url映射到另一个指定的url,即可用于拦截某一请求,并重定向到本地的资源或其他服务器地址 fiddler常用于: 解析请求:如:解析http请求状态,请求头,请求正文, ...

  10. Windows 手动安装 Apache24 web服务器

    文章更新于:2020-02-18 按照惯例,需要的文件附上链接放在文首 文件名:httpd-2.4.41-o111c-x64-vc15-r2.7z 文件大小:6.1MB 下载链接:https://ww ...