zoj --1649

//使用了优先队列
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int x[]={-1,1,0,0},y[]={0,0,-1,1};
int visited[220][220],n,m,x2,y2,ok,min1;
char map[220][220];

typedef struct node1
{
   int x,y,t;
}Q;
Q node; 

priority_queue<node1> q;  ; //声明结构体优先队列
bool operator<(Q q1,Q q2)//重载"<"运算符 
{
     return q1.t>q2.t;//声明为小的优先,注意与sort相反 
}

int  bfs()
{
    int a,b,t1,x1,y1;
    while(!q.empty())
    {
         node=q.top();
         q.pop();
         t1=node.t;
         x1=node.x;y1=node.y;
         for(int k=0;k<4;k++)
         {
             a=x1+x[k];b=y1+y[k];
             if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]!='#'&&!visited[a][b])
             {
                if(map[a][b]=='.')
                {
                   visited[a][b]=1;
                   node.x=a;node.y=b;node.t=t1+1;
                   q.push(node);
                }
                else if(map[a][b]=='x')
                {
                   visited[a][b]=1;
                   node.x=a;node.y=b;node.t=t1+2;
                   q.push(node);                  
                }
                else if(map[a][b]=='a')
                      return t1+1;
             } 
         }
    } 
}
int main()
{
    int i,j,x1,y1;
    while(cin>>n>>m)
    {
        for(i=0;i<n;i++)
            cin>>map[i];
        min1=-1;
        while(!q.empty()) q.pop(); //清空队列 
        memset(visited,0,sizeof(visited));
        for(i=0;i<n;i++)
           for(j=0;j<n;j++)
           {
               if(map[i][j]=='r') //将要搜索的所有的点都入队,然后依次搜索 
               {
                   map[i][j]='.'; 
                   node.x=i;
                   node.y=j;
                   node.t=0;
                   q.push(node);
                   visited[i][j]=1;
               }
           }
       /* min1=bfs(); 
        if(min1==-1)
          cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        else 
          cout<<min1<<endl;*/
        int ans=-1;
       	ans = bfs();
		if( ans != -1 )
			printf("%d/n",ans);
		else
			printf("Poor ANGEL has to stay in the prison all his life./n");
    }
 return 0;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值