#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int tu[300][300];
bool vis[300][300];
const int nil=(1<<29);
struct my{
int x;
int y;
int dirt;
bool operator<(const my &a)const{return dirt>a.dirt;}
};
int m,n;
//@==1
//#==2
//x==3
int dir[5][5]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
int sx,sy,ex,ey;
int ans[300][300];
priority_queue<my>Q;
bool bfs(){
while(!Q.empty()) Q.pop();
my u;
u.dirt=0;
u.x=sx;
u.y=sy;
Q.push(u);
while(!Q.empty()){
my v=Q.top();
Q.pop();
for (int i=1;i<=4;i++){
u.dirt=v.dirt+1;
int dx=v.x+dir[i][0];
int dy=v.y+dir[i][1];
if((tu[dx][dy]!=3&&dx<=m&&dy<=n&&dx>0&&dy>0)){
// printf("%d %d %d\n",dx,dy,ans[ey][ex]);
u.x=dx;
u.y=dy;
if(tu[dx][dy]==1) {
u.dirt;
Q.push(u);
}
if(tu[dx][dy]==2){
++u.dirt;
Q.push(u);
}
if(dx==ex&&dy==ey) {
printf("%d\n",u.dirt);
return true;
}
tu[dx][dy]=3;
}
}
}
return false;
}
int main(){
int t;
char c;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
scanf("%d%d",&m,&n);
scanf("%c",&c);
for (int i=1;i<=m;i++){
for (int j=1;j<=n;j++){
scanf("%c",&c);
if(c=='@') tu[i][j]=1;
if(c=='x') tu[i][j]=2;
if(c=='#') tu[i][j]=3;
if(c=='a') {
sx=i,sy=j;
tu[i][j]=3;
}
if(c=='r') {
ex=i,ey=j;
tu[i][j]=1;
}
}
scanf("%c",&c);
}
tu[sx][sy]=3;
if(!bfs())
printf("Impossible\n");
}
return 0;
}
转载于:https://www.cnblogs.com/lmjer/p/7852971.html