# include <iostream> # include <queue> # include <string.h> using namespace std; int map[302][302]; bool vis[302][302]; # include <stack> struct data { int x, y, count; bool operator < (data a)const { return count>a.count; } }dir[302][302]; int main() { int m, n, i, j, flag, t; int fang[4][2]={0, 1, 0, -1, 1, 0, -1, 0}; priority_queue<data> s; stack<data> list; data b,a,end; char c; // freopen("in.txt", "r", stdin); while (cin>>m>>n&&(m+n)) { memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); for (i = 1; i<=m; i++) { for (j = 1; j<=n; j++) { cin>>c; if (c=='Y'){map[i][j]=0;b.x=i;b.y=j;b.count=0;s.push(b);vis[i][j]=1;} if (c=='S'||c=='R') {map[i][j]=-1;} if (c=='B') map[i][j]=2; if (c=='E') map[i][j]=1; if (c=='T') {map[i][j]=1;end.x=i;end.y=j;} } } while (!s.empty()) { b = s.top(); s.pop(); flag = 0; for (i = 0; i<4; i++) { a.x=b.x+fang[i][0];a.y=b.y+fang[i][1]; if (map[a.x][a.y]<=0||vis[a.x][a.y]) {continue;} vis[a.x][a.y]=1; a.count=b.count+map[a.x][a.y]; dir[a.x][a.y].x=b.x; dir[a.x][a.y].y=b.y; if (a.x==end.x&&a.y==end.y) {flag=1;break;} s.push(a); } if (flag) break; } if (!flag) cout<<"-1"<<endl; else cout<<a.count<<endl; while (!s.empty()) s.pop(); while (end.x!=0||end.y!=0) { list.push(end); t = end.x; end.x=dir[end.x][end.y].x; end.y=dir[t][end.y].y; } while (!list.empty()) { cout<<list.top().x<<" "<<list.top().y<<endl; list.pop(); } } return 0; }
作者:liu_yutao 发表于2013-4-12 17:22:29 原文链接
阅读:0 评论:0 查看评论