hdu 2757 优先队列

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#define inf 8000
using namespace std;
int r,c;
int rs,cs,re,ce;
int step[1100][1100];
int map[1100][1100];
int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
struct node{
   int x,y;
   int t;
   friend bool operator <(node a,node b){
      return a.t>b.t;

   }//优先队列的关键,先考虑步数小的出队列

};
bool is_ok(node a){
   if(a.t<step[a.x][a.y]&&a.x>=1&&a.x<<r&&a.y>=1&&a.y<=c)
      return 1;
    else
      return 0;
}
void bfs(){
    priority_queue<node> Q;
    memset(step,inf,sizeof(step));
    node s;
    s.x=rs;
    s.y=cs;
    s.t=0;
    step[rs][cs]=0;
    Q.push(s);
    while(!Q.empty()){
          node h=Q.top();
          Q.pop();
          if(h.x==re&&h.y==ce)
            return;
          for(int i=0;i<8;i++){
               node u;
               u.x=h.x+dir[i][0];
               u.y=h.y+dir[i][1];
               u.t=h.t;
               if(map[h.x][h.y]!=i)
                  u.t+=1;
               if(is_ok(u)){

                  step[u.x][u.y]=u.t;
                  Q.push(u);

               }



          }




    }



}
int main(){
    while(cin>>r>>c){

        for(int i=1;i<=r;i++){
          for(int j=1;j<=c;j++){
              scanf("%1d",&map[i][j]);//神奇
          }

        }
          int n;
          cin>>n;
          while(n--){
              cin>>rs>>cs>>re>>ce;
              if(rs==re&&cs==ce)
                {
                    cout<<0<<endl;
                    continue;
                }
              bfs();
              cout<<step[re][ce]<<endl;

          }






    }




}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值