来源poj2312

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

8

搜索T的位置,输出要走的步数,用只要把bfs的队列变成优先队列就可以了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=3e2+10;
char MAP[N][N];
struct node
{
int x,y,ans;
friend bool operator <(node a,node b){ return a.ans>b.ans; }
node(int a=0,int b=0){ x=a;y=b;ans=0; }
};
int a[4][2]={-1,0,1,0,0,1,0,-1};
priority_queue<node>q;
bool checkt(node a)
{
return (MAP[a.x][a.y]=='T');
}
int visit[N][N];
int bfs()
{
node t,tt;
while(!q.empty())
{
t=q.top();
q.pop();
visit[t.x][t.y]=1;
rep(i,0,4)
{
tt.ans=t.ans+1;
tt.x=t.x+a[i][0];
tt.y=t.y+a[i][1];
if(!visit[tt.x][tt.y])
{
if(checkt(tt))
return tt.ans;
if(MAP[tt.x][tt.y]=='E')
q.push(tt);
else if(MAP[tt.x][tt.y]=='B')
{
tt.ans++;
q.push(tt);
}
visit[tt.x][tt.y]=1;
}
}
}
return -1;
}
void display(int n,int m)
{
mm(visit,0);
while(!q.empty()) q.pop();
int x,y;
rep(i,1,n+1)
rep(j,1,m+1)
if(MAP[i][j]=='Y')
{ x=i;y=j;}
q.push(node(x,y));
prf(bfs());
}
int main()
{
int n,m;
while(~scff(n,m)&&n&&m)
{
mm(MAP,'S');
rep(i,1,n+1)
{
sf("%s",MAP[i]+1);
MAP[i][m+1]='S';
}
display(n,m);
}
return 0;
}

B - Battle City bfs+优先队列的更多相关文章

  1. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  2. C - Battle City BFS+优先队列

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  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自学笔记11

    JAVA自学笔记11 1:Eclipse的安装 2:用Eclipse写一个HelloWorld案例,最终在控制台输出你的名字 A:创建项目 B:在src目录下创建包.cn.itcast C:在cn.i ...

  2. linux常用命令和关闭防火墙

    linux常用命令和关闭防火墙   2.        linux常用命令 Mkdir 创建 Rm -rf 删除 Chmod -R 777 权限 Mysql -uroot -r quit退出 find ...

  3. Mybatis : "less than" issue in Select annotations

    I am using java7, spring 3 and mybatis Pom.xml <org.mybatis-version>3.2.8</org.mybatis-vers ...

  4. Hadoop的启动和停止说明

    Hadoop的启动和停止说明   sbin/start-all.sh 启动所有的Hadoop守护进程.包括NameNode. Secondary NameNode.DataNode.ResourceM ...

  5. ECharts JS应用:图表页面实现

    因为要统计数据进行图表展示,所以就简单学习了 ECharts JS 的应用.它是一个纯Javascript图库,它依赖于一个轻量级的Canvas库 ZRender,并提供直观.生动.交互式和高度可定制 ...

  6. MyBatis项目快速搭建及MySQL一个Statement支持多条命令参数

    一.简述 本文以笔记的形式,记录一个基本Mybatis项目的使用,方便后期项目使用到相关配置时直接复制使用. 二.项目结构 pom.xml中的依赖 <!-- https://mvnreposit ...

  7. C#之值类型和引用类型

    本文意在巩固基础知识,并不是对其进行深入剖析,还望理解.本文是对此项知识的整理文,有些内容来源于网络,其他为博主原创,所以难免会有一些小得瑕疵,敬请谅解.所有示例均是博主测试过的,如有转载请标明出处, ...

  8. 【理论面试篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题

    ##2017.10.30收集 面试技巧 5.1 面试形式 1)        一般而言,小公司做笔试题:大公司面谈项目经验:做地图的一定考算法 2)        面试官喜欢什么样的人 ü  技术好. ...

  9. c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习

    c#中@标志的作用   参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/toke ...

  10. Vmware 虚拟机无法启动

    问题背景: 自己的电脑坏了,用的事小伙伴的电脑,安装VMware 软件,然后创建虚拟机(放在移动硬盘上).在操作虚拟主机的时候,中间不小心碰到了移动硬盘, 然后移动硬盘就掉线了.这个时候再去启动虚拟主 ...