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. React项目结构

    任何一种语言.框架,在真正上手的时候,多多少少会想想怎么安排项目结构(正所谓磨刀不误砍柴工),React也不例外. google了下,拿下面3篇博客来说道说道. (1) how-to-better-o ...

  2. javascript 返回上一页面

    <a href="<a href="javascript :history.back(-1)">返回上一页</a>或<a href=& ...

  3. MySQL 导入导出命令(转载)

    导出数据: mysqldump --databases -u root -p密码 数据库名> /root/guogl/XXX.sql 从sql文件导入数据: mysql -u root -p密码 ...

  4. Lance老师UI系列教程第九课->高仿比特币监控大师

    http://blog.csdn.net/lancees/article/details/22898971

  5. arcgis for javascript 隐藏或显示底图

    arcgis for javascript展示地图的时候,有图层的概念,一层一层的,类似photoshop,在应用界面上控制图层的显隐,是极常见的功能. 但是,如果是控制底图的显示或隐藏呢,怎么搞? ...

  6. arm处理器的历史及现状

    1 arm处理器的发展历史 arm1 arm2 arm3 arm6 arm7 arm9 arm11 arm cortex 2 arm处理器现状 arm cortex A a即application,即 ...

  7. Protocol_ISIS

    集成ISIS协议 作者:Danbo 2015-7-5 ISIS的意思是表示中间系统,并且是为OSI无连接网络协议(OSI Conectionless Network Protocol,CLNP)设计的 ...

  8. Android Weekly Notes Issue #246

    Android Weekly Issue #246 February 26th, 2017 Android Weekly Issue #246 本期内容包括: RecyclerView上的Shared ...

  9. src源dst目标

    dst是destination的缩写,表目的 src是source的缩写,表源

  10. 搭建iis本地测试服务器

    在“开始”选择 “控制面板”,默认是以“类别”显示,   改成“小图标”显示   选择“程序和功能”   进入界面后,点击“启动或关闭Windows功能”   然后勾选图中的两个选框,注意一定要显示为 ...