博客
关于我
POJ 2312:Battle City(BFS)
阅读量:217 次
发布时间:2019-02-28

本文共 3114 字,大约阅读时间需要 10 分钟。

                                            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 4YBEBEERESSTE0 0

Sample Output

8

题意

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

AC代码

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define ms(a) memset(a,0,sizeof(a))#define pi acos(-1.0)#define INF 0x3f3f3f3fconst 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
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<
<
>n>>m) { if(n==0&&m==0) break; ms(vis); ms(ch); int x1,x2,y1,y2; for(int i=0;i
>ch[i]; for(int i=0;i

 

转载地址:http://dcbp.baihongyu.com/

你可能感兴趣的文章
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>