poj2312

#include
#include
#include
#include
#include
#include
using namespace std;


struct node{
int x,y,s;
bool operator<(const node &a)const{
   return s>a.s;
}
};


int n,m,bx,by,ex,ey,dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1},vis[301][301];
char map[301][301];


void solve(){
priority_queue q;
node a;
a.x = bx;
a.y = by;
a.s = 0;
q.push(a);
int flag = 0;
while(q.size()){
a = q.top(); q.pop();
if(a.x == ex&&a.y == ey){
if(a.s == 0)
break;
cout< flag = 1;
break;
}
for(int i = 0;i<4;++i){
int xx = a.x+dx[i];
int yy = a.y+dy[i];
if(xx >= 0&&xx < n&&yy >=0 &&yy < m&&map[xx][yy] != 'S'&&map[xx][yy] != 'R'&&vis[xx][yy] == 0){
vis[xx][yy] = 1;
node c;
c.x = xx;
c.y = yy;
if(map[xx][yy] == 'B')
c.s = a.s+2;
else
c.s = a.s+1;
q.push(c);
}
}
}
if(flag == 0)
cout<<"-1\n";
}


int main(){
while(cin>>n>>m){
if(n == 0&&m == 0) break;
for(int i = 0;i for(int j = 0;j cin>>map[i][j];
if(map[i][j] == 'Y'){
bx = i;
by = j;
}
if(map[i][j] == 'T'){
ex = i;
ey = j;
}
}
}
memset(vis,0,sizeof(vis));
vis[bx][by] = 1;
solve();
}
return 0;
}

你可能感兴趣的:(poj2312)