http://noi.openjudge.cn/ch0205/4980/
这是题
#include<bits/stdc++.h>
using namespace std;
char mg[300][300];
int n,m,s,jh,jl;
struct node{
int h,l,b;
};
queue<node> q;
int bfs(){
while(!q.empty()){
node t;
t=q.front();
q.pop();
if(t.l==jl&&t.h==jh){
return t.b;
}
t.b++;
if(mg[t.h][t.l]=='*'){
q.push(t);
mg[t.h][t.l]='#';
continue;
}
if(t.l+1<m&&mg[t.h][t.l+1]=='@'||mg[t.h][t.l+1]=='x'){
t.l++;
q.push(t);
if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
else mg[t.h][t.l]='#';
t.l--;
}
if(t.h+1<n&&mg[t.h+1][t.l]=='@'||mg[t.h+1][t.l]=='x'){
t.h++;
q.push(t);
if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
else mg[t.h][t.l]='#';
t.h--;
}
if(t.h-1>=0&&mg[t.h-1][t.l]=='@'||mg[t.h-1][t.l]=='x'){
t.h--;
q.push(t);
if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
else mg[t.h][t.l]='#';
t.h++;
}
if(t.l-1>=0&&mg[t.h][t.l-1]=='@'||mg[t.h][t.l-1]=='x'){
t.l--;
q.push(t);
if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
else mg[t.h][t.l]='#';
t.l++;
}
}
return -1;
}
int main(){
cin>>s;
for(int i=0;i<s;i++){
cin>>n>>m;
for(int j=0;j<n;j++){
for(int o=0;o<m;o++){
cin>>mg[j][o];
if(mg[j][o]=='r'){
node t;
t.h=j;
t.l=o;
t.b=0;
q.push(t);
mg[j][o]='#';
}
if(mg[j][o]=='a'){
jh=j;
jl=o;
mg[j][o]='@';
}
}
}
int u=bfs();
if(u<0){
cout<<"Impossible"<<endl;
}else{
cout<<u<<endl;
}
while(!q.empty()) q.pop();
}
return 0;
}
这是代码
求调
感谢各位dalao