Battle City

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

又是一道BFS+优先队列的题,和之前的WAR CHESS类似,但判断条件较少。这次主要是想作个比较,用暴力DFS跑一边,果断TLE。。BFS+优先队列只遍历最少步数之内的点一次,时间效率快很多

dfs length 861 time 1s+

bfs length 1469 time 0.016s memory 1m

Status Time Limit Exceeded
Length 861
Lang G++
Submitted 2017-07-22 22:22:12
Shared
RemoteRunId 17284030
Status Accepted
Time 16ms
Memory 1028kB
Length 1469
Lang G++
Submitted 2017-07-22 22:22:58
Shared
RemoteRunId 17284040
#include<stdio.h> 
#include<string.h>
char a[][];
int b[][];
int n,m,f,bx,by,min;
void dfs(int x,int y,int s)
{
if(x<||y<||x>=n||y>=m) return;
if(b[x][y]==) return;
if(a[x][y]=='T'){
f=;
if(s<min) min=s;
return;
}
else if(a[x][y]=='S'){
b[x][y]=;
return;
}
else if(a[x][y]=='R'){
b[x][y]=;
return;
}
else if(a[x][y]=='B'){
s++;
}
b[x][y]=;dfs(x+,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y+,s+);b[x][y]=;
b[x][y]=;dfs(x-,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y-,s+);b[x][y]=;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
f=;
min=;
dfs(bx,by,);
if(f==) printf("%d\n",min);
else printf("-1\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
int n,m,bx,by; struct Node{
int x,y,s;
friend bool operator<(Node a,Node b)
{
return a.s>b.s;
}
}node; void bfs()
{
int i,f=,tx,ty;
priority_queue<Node> q;
node.x=bx;
node.y=by;
node.s=;
b[bx][by]=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
tx=q.top().x+t[i][];
ty=q.top().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='T'){
printf("%d\n",q.top().s+);
f=;
return;
}
else if(a[tx][ty]=='S'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='R'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='B'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
else if(a[tx][ty]=='E'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
}
q.pop();
}
if(f==) printf("-1\n");
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
bfs();
}
return ;
}

POJ - 2312 Battle City BFS+优先队列的更多相关文章

  1. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  2. poj 2312 Battle City

    题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...

  3. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

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

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

  5. poj 2312 Battle City(优先队列+bfs)

    题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...

  6. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  7. Battle City 优先队列+bfs

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

  8. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

    题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...

  9. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

随机推荐

  1. docker&k8s填坑记

    本篇主要用于记录在实施docker和kubenetes过程中遇到的一个问题和解决办法. 本节部分内容摘自互联网,有些部分为自己在测试环境中遇到到实际问题,后面还会根据实际情况不断分享关于docker/ ...

  2. eclipse的快捷键(常用)

    1. Ctrl+O 显示类中方法和属性的大纲,能快速定位类的方法和属性,在查找Bug时非常有用. 2. Ctrl+M 窗口最大化和还原,用户在窗口中进行操作时,总会觉得当前窗口小(尤其在编写代码时), ...

  3. JavaScript读书笔记(3)-操作符、语句和函数

    1.  操作符 (1)       一元操作符 前置递增和递减操作符,变量的值都是在语句被求值以前改变的:后置相反 (2)       位操作符 在ECMAScript中,对数值进行位操作时,会发生以 ...

  4. vue 生命周期钩子

    每个vue实例被创建时都会经历一系列初始化的过程,像是一个生命从无到有的过程,所以叫生命周期,而这个过程都有对应的不同阶段,也就对应了生命周期不同的钩子函数,这些生命周期函数,作为vue实例的属性使用 ...

  5. Java基础知识查漏 一

    Java基础知识查漏 一 Jdk和jre Jdk是java程序设计师的开发工具,只要包含编译程序,jvm和java函数库 Jre中只有jvm和java函数库,没有编译程序的相关工具,适合只运行不撰写j ...

  6. java object monitor

    1 什么是java object monitor 每个java对象头中都有锁状态位标记.java中在使用synchronize同步的时候,肯定是涉及到某个对象的锁.因此,在考虑同步的时候,首先要想到是 ...

  7. WildFly JBoss 应用程序服务器

    https://en.wikipedia.org/wiki/WildFly [实现基于面向服务的架构SOA的web应用和服务] WildFly,[1] formerly known as JBoss ...

  8. h5 placeholder 设置无效

    下面设置方式无效: ::-webkit-input-placeholder { /* WebKit browsers */ color: #999; } :-moz-placeholder { /* ...

  9. 很好的 DHCP协议与dhcpcd分析【转】

    本文转载自:http://blog.csdn.net/gjsisi/article/details/18052369 第一部分 DHCP工作过程 DHCP的工作过程主要分为以下六个阶段:     发现 ...

  10. jquery特效(2)—选项卡

    最近公司有个页面正好用到了选项卡,我就写了一下,感觉还不错,都挺简单的. 下面来看动态效果: 一.主体程序 <!DOCTYPE html> <html> <head> ...