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. 加油站问题 Gas Station

    2019-06-01 17:09:30 问题描述: 问题求解: 其实本题本质上是一个数学题. [定理] 对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元 ...

  2. tensorflow 控制流操作,条件判断和循环操作

    Control flow operations: conditionals and loops When building complex models such as recurrent neura ...

  3. React Hooks 实现react-redux

    Redux 是目前 React 系统中最常用的数据管理工具,它落实并发扬了 Flux 的数据单向流动模式,被实践证明为一种成熟可用的模式. 尽管承受着一些非议,Redux 在 React 数据管理界的 ...

  4. Tainted canvases may not be exported的问题解决

    项目里使用到用canvas生成海报,在toDataURL报了这个错误Tainted canvases may not be exported. 原因就在于使用了跨域的图片,所以说是被污染的画布.解决方 ...

  5. office2010安装与破解,笔者亲测可用!!!!!!

    我们首先需要准备office2010安装包与破相应的破解软件.软件包的获取方式:扫码关注[猿成长],,回复 office2010安装,即可获取,下载解压后文件目录结构如下图所示: 打开安装程序文件夹, ...

  6. iPhone连接到Mac上叮叮叮断断续续响个不停的解决办法

    一.推荐方式 1.让iPhone和Mac通过数据线连接(对,就是连着) 2.打开终端,执行如下命令: sudo killall -STOP -c usbd 3.一分钟内,iPhone即可连上Mac 二 ...

  7. flask中温柔显示404等错误

    写下下面两个视图函数,然后在模板中写下错误时展现的内容,当然模板名,函数名是可以改的哟@app.errorhandler(404)def page_not_found(error): return r ...

  8. js中使用Timer来计时程序执行时 - [javascript] - [开发]

    在我们开发过程中,我们也在不断的学习,以及优化自己的代码质量. 我们时常需要一个计时器,来对代码某段或者某些段执行进行计时,以评估代码运行质量,考虑是否优化. 以及优化后的直观对比. JavaScri ...

  9. Ptask

    这是一款非常弱鸡的小程序,不喜勿喷 你们好!如在使用中有bug或者有您宝贵的建议请在下方评论区留言或者投递至我的邮箱:Mj_Ymr@outlook.com. 那么我也会不断更新,并在这里贴上各版本的下 ...

  10. Jmeter压力测试笔记(6)性能调测-压力并发-模拟生产环境数据

    问题原因找到了,那就好办了. 找到阿里云技术人员,让他们强行给我们上架了一个共享代理模式的Redis. 并重新进行压力测试. 哦豁~ 开心,压力测试顺利,异常率大大降低实际为: 数据库DBA反馈,数据 ...