题意:从Y走到T,B点要把它开枪消灭掉才能走,S和R不能走。
思路:BFS,将步数作为优先队列的关键字,E点直接一步走到,B点两步走到。
/*I have given up the treatment-_-||*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef long long LL;
const int maxn=310;
const int maxm=310;
const int inf=1e8;
const int mod=10000;
int n,m;
char mat[maxn][maxn];
int vis[maxn][maxn];
struct Node
{
int x;
int y;
int step;
bool operator < (const Node &tt) const {
return step>tt.step; //step小的在前面
}
};
bool ok(int a,int b)
{
if(a<1||b<1|a>n||b>m) return false;
return true;
}
int bfs(Node s)
{
memset(vis,0,sizeof(vis));
vis[s.x][s.y]=1;
int i,j;
priority_queue<Node> que;
que.push(s);
while(!que.empty()){
Node temp=que.top();
que.pop();
int a,b;
a=temp.x;
b=temp.y;
//printf("*%d %d\n",a,b);
if(ok(a-1,b)&&!vis[a-1][b]&&mat[a-1][b]!='S'&&mat[a-1][b]!='R'){
Node tp;
tp.x=a-1;
tp.y=b;
if(mat[a-1][b]=='T') return temp.step+1;
else if(mat[a-1][b]=='E'){
tp.step=temp.step+1;
vis[a-1][b]=1;
que.push(tp);
}
else{
tp.step=temp.step+2;
vis[a-1][b]=1;
que.push(tp);
}
}
if(ok(a+1,b)&&!vis[a+1][b]&&mat[a+1][b]!='S'&&mat[a+1][b]!='R'){
Node tp;
tp.x=a+1;
tp.y=b;
if(mat[a+1][b]=='T') return temp.step+1;
else if(mat[a+1][b]=='E'){
tp.step=temp.step+1;
vis[a+1][b]=1;
que.push(tp);
}
else{
tp.step=temp.step+2;
vis[a+1][b]=1;
que.push(tp);
}
}
if(ok(a,b-1)&&!vis[a][b-1]&&mat[a][b-1]!='S'&&mat[a][b-1]!='R'){
Node tp;
tp.x=a;
tp.y=b-1;
if(mat[a][b-1]=='T') return temp.step+1;
else if(mat[a][b-1]=='E'){
tp.step=temp.step+1;
vis[a][b-1]=1;
que.push(tp);
}
else{
tp.step=temp.step+2;
vis[a][b-1]=1;
que.push(tp);
}
}
if(ok(a,b+1)&&!vis[a][b+1]&&mat[a][b+1]!='S'&&mat[a][b+1]!='R'){
Node tp;
tp.x=a;
tp.y=b+1;
if(mat[a][b+1]=='T') return temp.step+1;
else if(mat[a][b+1]=='E'){
tp.step=temp.step+1;
vis[a][b+1]=1;
que.push(tp);
}
else{
tp.step=temp.step+2;
vis[a][b+1]=1;
que.push(tp);
}
}
}
return -1;
}
int main()
{
//freopen ("in.txt" , "r" , stdin);
//freopen ("output.txt" , "w" , stdout);
int i,j;
while(scanf("%d%d",&n,&m)&&(n||m)){
for(i=1;i<=n;i++){
scanf("%s",mat[i]+1);
}
Node start;
Node target;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(mat[i][j]=='Y'){
start.x=i;
start.y=j;
start.step=0;
break;
}
}
}
int ans=bfs(start);
printf("%d\n",ans);
}
return 0;
}