简单题
#include
#include
#include
using namespace std;
const int MAX=;
const int inf=;
int tim[MAX][MAX];
char maze[MAX][MAX];
int n,m;
int ai,aj;
typedef struct c{
int i,j,ti;
bool operator <(const c &A)const {
if(ti>A.ti) return true;
return false;
}
}Node;
Node tmp,pushed;
priority_queue
int dir[][]={,,,-,,,-,};
bool ok(int i,int j){
if(i>=&&i
return true;
return false;
}
bool bfs(){
int ti,tj;
while(!que.empty()){
tmp=que.top();
que.pop();
if(maze[tmp.i][tmp.j]=='r'){
printf("%d\n",tmp.ti);
return true;
}
for(int i=;i<;i++){
ti=tmp.i+dir[i][];
tj=tmp.j+dir[i][];
if(ok(ti,tj)){
if(maze[ti][tj]=='.'||maze[ti][tj]=='r'){
if(tmp.ti+<tim[ti][tj]){
tim[ti][tj]=tmp.ti+;
pushed.i=ti; pushed.j=tj; pushed.ti=tmp.ti+;
que.push(pushed);
}
}
else if(maze[ti][tj]=='x'){
if(tmp.ti+<tim[ti][tj]){
tim[ti][tj]=tmp.ti+;
pushed.i=ti; pushed.j=tj; pushed.ti=tmp.ti+;
que.push(pushed);
}
}
}
}
}
return false;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%s",maze[i]);
for(int j=;j<m;j++){
if(maze[i][j]=='a'){
ai=i; aj=j;
}
tim[i][j]=inf;
}
}
tim[ai][aj]=;
tmp.i=ai; tmp.j=aj; tmp.ti=;
que.push(tmp);
if(!bfs()){
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
while(!que.empty())
que.pop();
}
return ;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章