Battle City

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9885   Accepted: 3285

Description

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

题意

n*m的矩阵,Y代表起点,T代表终点,R不能通过,走E需要一步,B需要两步。求从起点到终点的最短距离。如果不能到达,输出-1

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
char ch[maxn][maxn];
using namespace std;
int place[5][2]={1,0,-1,0,0,1,0,-1};
int vis[maxn][maxn];
int n,m;
struct node
{
int x,y,dis;
};
bool operator < (const node a,const node b)
{
return a.dis>b.dis;
}
void bfs(int a,int b,int c,int d)
{
ms(vis);
vis[a][b]=1;
priority_queue<node> que;
node start,end;
start.x=a;
start.y=b;
start.dis=0;
que.push(start);
int ans=-1;
while(!que.empty())
{
start=que.top();
que.pop();
if(start.x==c&&start.y==d)
{
ans=start.dis;
break;
}
for(int i=0;i<4;i++)
{
end.x=start.x+place[i][0];
end.y=start.y+place[i][1];
if(ch[end.x][end.y]=='R'||ch[end.x][end.y]=='S')
continue;
if(end.x<0||end.x>=n||end.y<0||end.y>=m)
continue;
if(vis[end.x][end.y])
continue;
if(ch[end.x][end.y]=='E'||ch[end.x][end.y]=='T')
end.dis=start.dis+1;
if(ch[end.x][end.y]=='B')
end.dis=start.dis+2;
que.push(end);
vis[end.x][end.y]++;
}
}
cout<<ans<<endl;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
ms(vis);
ms(ch);
int x1,x2,y1,y2;
for(int i=0;i<n;i++)
cin>>ch[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ch[i][j]=='Y') {x1=i;y1=j;}
if(ch[i][j]=='T') {x2=i;y2=j;}
}
}
bfs(x1,y1,x2,y2);
}
return 0;
}

POJ 2312:Battle City(BFS)的更多相关文章

  1. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  2. 九度OJ 1335:闯迷宫 (BFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1782 解决:483 题目描述: sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫. sun的室友在帮电脑节设计 ...

  3. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  4. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. POJ 2435Navigating the City(bfs)

    题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...

  6. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  7. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  8. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  9. POJ 3183:Stump Removal(模拟)

    http://poj.org/problem?id=3183 题意:有n个树桩,分别有一个高度h[i],要用Bomb把树桩都炸掉,如果炸的位置的两边树桩高度小于Bomb炸的树桩高度,那么小于树桩高度的 ...

随机推荐

  1. BN层

    论文名字:Batch Normalization: Accelerating Deep Network Training by  Reducing Internal Covariate Shift 论 ...

  2. 利用Chrome的Heap Snapshot功能分析一个时间段内的内存占用率

    在下图测试代码第13行和第16行设断点. 以调试方式运行,首先断点在第13行处触发: 打开Chrome开发者工具,点击Profiles tab, 再点击按钮"Take Snapshot&qu ...

  3. 使用SFTP工具相关问题

    1.使用SFTP工具,填写ip,端口都正确但是连接不上?         答:请统一使用 filezilla工具进行连接,环境搭建使用该工具进行测试和使用.           2.使用SFTP工具访 ...

  4. angular5 directive和component的区别

    指令分为三类,组件,属性指令和结构性指令 组件(Component directive):UI组件,继承于Directive: 属性指令(Attribute directive):改变组件的样式: 结 ...

  5. Illumina Sequence Identifiers 序列标识符 index详解

    大家基本都知道什么是 FASTA 和 FastQ 格式了,但这是不够的. 我们还需要了解世界上最大的测序公司自己定制的 FastQ 格式,因为你可能会经常用到,有时还会亲自去处理它们. 本文主题:Il ...

  6. Confluence 6 完成你的任务

    很好,宇航员们,你已经令人钦佩的展示了你自己的.我们确定你新招募的员工已经对你了解的 Confluence 知识感到赞叹. 在这个指南中,我们已经完成了: 在主面板中对 Confluence 的功能进 ...

  7. Java 历史

    James Gosling 最初开始 Java 语言项目是在 1991 年的 7 月.Java 被用在他的许多 set-top box 工程中.这个语言最开始的时候被称为 Oka,这个是因为 Jame ...

  8. 『Sklearn』框架自带数据集接口

    自带数据集类型如下: # 自带小型数据集# sklearn.datasets.load_<name># 在线下载数据集# sklearn.datasets.fetch_<name&g ...

  9. shell里的/dev/null 2>&1详解

    shell中可能经常能看到: >/dev/null 2>&1 命令意思是:标准输出stdout 和标准错误输出stderr 也重定向到空设备文件,即不显示输出信息 分解这个组合:“ ...

  10. 在Eclipse中Attach Source

    在Eclipse中,有时需要查看类.方法和变量的声明和定义的源代码. 但是在F3查看一些在JDK库中声明/定义的类.方法和 变量的源代码时,Eclipse给你打开的却是相应的.class文件(byte ...