题目描述
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
个人思考:这个地方为什么要以s较小的排序呢?
优先队列、、、、top
blablabla
----------
### 算法1
##### (暴力枚举) $O(n^2)$
#include<iostream>
#include <algorithm>
#include <cstring>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define _for(i,a,b) for( int i=(a); i<=(b); ++i)
#define jfor(i,a,b) for(int i=x;i<=n;i+= lowbit(i))
typedef pair<int,int> PII;
typedef pair<PII,int> pii;
const int N=600010,M=310,inf=0x3f3f3f3f,mod=1e9+7,mit=1000*100*2;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int dx[2]={-1,1};
int n,m,dist[N],t,res,g[1][1],p[N],s,cnt,d[N],v[N],ans;
int e[N*2],ne[N],h[N],idx,w[N];
bool st[M][M],flag;
char c[M][M];
struct Edge
{
int a,b,w;
bool operator<(const Edge &W)const
{
return w<W.w;
}
}edges[10010];
struct node
{
int x,y,s;
friend bool operator<(node a,node b)
{
return a.s>b.s; //优先走步数小的
}
};
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
bool check(int x,int y)
{
if(x>0&&x<=n&&y>0&&y<=m)
return true ;
return false;
}
void bfs(int x1,int y1)
{ flag= false;
node now,next;
priority_queue<node> q;
q.push({x1,y1,0});
while(!q.empty())
{
node tp=q.top();
q.pop();
for(int i=0;i<4;i++)
{
next.x=tp.x+dir[i][0];
next.y=tp.y+dir[i][1];
if(check(next.x,next.y)&&!st[next.x][next.y])
{
if(c[next.x][next.y]=='B')
next.s=tp.s+2,st[next.x][next.y]=true,q.push(next);
else if(c[next.x][next.y]=='E')
next.s=tp.s+1,st[next.x][next.y]=true,q.push(next);
else if(c[next.x][next.y]=='T')
{
next.s=tp.s+1,flag=1;
cout<<next.s<<endl;
break;
}
}
}if(flag==1)
break;
}
if(flag==0)
cout<<-1<<endl;
}
int main() {
while(cin>>n>>m,n&&m)
{
memset(st,0,sizeof st);
memset(c,0,sizeof c);
int x1,x2,y1,y2;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>c[i][j];
if(c[i][j]=='Y')
x1=i,y1=j;
}
bfs(x1,y1);
}
return 0;
}
#### 时间复杂度
#### 参考文献
#### C++ 代码
blablabla
----------
### 算法2
##### (暴力枚举) $O(n^2)$
blablabla
#### 时间复杂度
#### 参考文献
#### C++ 代码
blablabla
```