# NYOJ - 1100 - WAJUEJI which home strong！(BFS变形,优先队列)

‘s’ 表示弟弟目前所在位置。
‘# ’表示此处为一座山。为了节省体力，不从此处通行。

‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。

`33 5#sVGFA##ZAlCDBC3 3sABABSABl3 3s#B###ABl`

`484-1`

`######sZl##AAA######`

`struct Point{int x,y,money;//重载小于运算符  friend  bool operator<(Point a,Point b){//money小的优先出队 return a.money>b.money; }};`

`#include<queue>#include<cstdio>#include<cstring>using namespace std;struct Point{int x,y,money;//重载小于运算符  friend  bool operator<(Point a,Point b){//money小的优先出队 return a.money>b.money; }};char map[105][105];int vis[105][105];int dir[4][2] = {0,1 ,0,-1 ,1,0 ,-1,0};int x1,y1,x2,y2,ans,n,m,t;void bfs(){if(x1==x2&&y1==y2){ans = 0;return ;}priority_queue<Point>q;//优先队列 money越小越好 Point v1;v1.x = x1;v1.y = y1;v1.money = 0;vis[x1][y1] = 1;q.push(v1);while(!q.empty()){Point v2;for(int i=0 ;i<4 ;i++){v2.x = q.top().x + dir[i][0];v2.y = q.top().y + dir[i][1];if(v2.x>=0&&v2.x<n&&v2.y>=0&&v2.y<m&&!vis[v2.x][v2.y]&&map[v2.x][v2.y]!='#'){if(v2.x==x2&&v2.y==y2){ans = q.top().money;break;}v2.money = q.top().money + (int)(map[v2.x][v2.y]-'A'+1);vis[v2.x][v2.y] = 1;q.push(v2);}}if(v2.x==x2&&v2.y==y2){return ;}q.pop();}}int main(){scanf("%d",&t);while(t--){memset(vis,0,sizeof(vis));ans = -1;scanf("%d%d",&n,&m);getchar();for(int i=0 ;i<n ;i++){gets(map[i]);}for(int i=0 ;i<n ;i++){for(int j=0 ;j<m ;j++){if(map[i][j]=='s'){x1 = i;y1 = j;}if(map[i][j]=='l'){x2 = i;y2 = j;}}}bfs();printf("%d\n",ans);}return 0;}`

0 0