hdu 2822 ~!!!!!!坑死我

首先 在此哀悼。。。  为我逝去的时间哀悼。。。  每一步都确定再去写下一步吧。。。日狗 

不过还是有点收获的。。  对优先队列的使用 有了进一步的理解

先上代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char mapp[1001][1001];
int vis[1001][1001],n,m,sx,sy,ex,ey;
struct node
{
 int x,y;
 int time;
 friend  bool operator < (node a,node b)
 {
    return a.time>b.time;
 }
};
int bfs()
{
 int i,j;
 priority_queue<node> q;
 memset(vis,0,sizeof(vis));
 vis[sx][sy]=1;
 node t,stu;
 stu.x=sx;
 stu.y=sy;
 stu.time=0;
 q.push(stu);
 while(!q.empty())
 {
  t=q.top();
  q.pop();
  if(t.x == ex&&t.y == ey) return t.time;
  for(i=0;i<4;i++)
  {
   stu.x=t.x+dir[i][0];
   stu.y=t.y+dir[i][1];
      if(vis[stu.x][stu.y]==1||stu.x<=0||stu.x>n||stu.y<=0||stu.y>m) continue;
    vis[stu.x][stu.y]=1;
    if(mapp[stu.x][stu.y]=='.')
    {
      stu.time=t.time+1;
   //   cout<<stu.x<<' '<<stu.y<<endl;
    }
    else stu.time=t.time;
   // if(mapp[stu.x][stu.y]=='X') stu.tmp=1;
    q.push(stu);
   
  }
 }
 return 0;
}
int main()
{
 while(~scanf("%d %d",&n,&m))
 {
  if(n==0||m==0) break;
  int i,j;
  for(i=1;i<=n;i++)
  {
        scanf("%s",mapp[i]+1);////这里比较重要  注意题目给的题目于自己写的地图的位置关系(这里相当于指针的应用。。) 
  }
  scanf("%d %d",&sx,&sy);
  scanf("%d %d",&ex,&ey);
     printf("%d ",bfs());
 }
 return 0;
}

总的来说 对于优先队列的有了更深的理解 (按权优先的策略 后来的点的位置可能更前)

原文地址:https://www.cnblogs.com/z1141000271/p/5498957.html