tjut 2822

来源:互联网 发布:欧洲为什么发达 知乎 编辑:程序博客网 时间:2024/06/03 22:41
#include <cstdio>  #include <queue>  #include <cstring>  #include <iostream>  #include <cstdlib>  #include <algorithm>  #include <vector>  #include <map>  #include <string>  #include <set>  #include <ctime>  #include <cmath>  #include <cctype>  using namespace std;  #define maxn 1005  #define LL long long  int cas=1,T;  char mapp[maxn][maxn];  int vis[maxn][maxn];  struct Node  {      char l;      int x,y,time;      friend bool operator < (const Node&a,const Node&b)      {return a.time > b.time;}  };  int n,m,startx,starty,endx,endy;  int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};  bool check(int x,int y)  {      if (x<1 || x>m || y<1 || y>n || vis[x][y])          return true;      return false;  }  int bfs()  {      memset(vis,0,sizeof(vis));      priority_queue<Node> q;      Node s,temp;      s.x=startx;      s.y=starty;      s.time=0;      vis[s.x][s.y]=1;      q.push(s);      while (!q.empty())      {          s = q.top();q.pop();          if (s.x==endx && s.y==endy)              return s.time;          for (int i = 0;i<4;i++)          {              temp.x = s.x+dir[i][0];              temp.y = s.y+dir[i][1];              if (check(temp.x,temp.y))                  continue;              if (mapp[temp.x][temp.y]=='X')                  temp.time = s.time+0;              else                  temp.time = s.time+1;              vis[temp.x][temp.y]=1;              q.push(temp);          }      }      return -1;  }  int main()  {      while (scanf("%d%d",&m,&n)!=EOF && m && n)      {          for (int i = 1;i<=m;i++)              scanf("%s",mapp[i]+1);          scanf("%d%d",&startx,&starty);          scanf("%d%d",&endx,&endy);          int ans = bfs();          printf("%d\n",ans);      }      //freopen("in","r",stdin);      //scanf("%d",&T);      //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);      return 0;  }

0 0
原创粉丝点击