简单题;
优先队列:如果为"."step+1,为”X“不加;搜素即可;
#include<stdio.h>
#include<queue>
#define N 1002
using namespace std;
struct node
{
int x,y,step;
friend bool operator<(node n1,node n2)
{
return n2.step<n1.step;
}
};
char map[N][N];
int a,b,sx,sy;
int tox[4][2]={0,1,0,-1,1,0,-1,0};
int bfs(int x,int y)
{
priority_queue<node>q;
node cur,next;
int k;
cur.x=x;
cur.y=y;
cur.step=0;
q.push(cur);
map[x][y]='*';
while(!q.empty())
{
cur=q.top();
q.pop();
for(k=0;k<4;k++)
{
x=next.x=cur.x+tox[k][0];
y=next.y=cur.y+tox[k][1];
if(x>=0&&x<a&&y>=0&&y<b&&map[x][y]!='*')
{
if(x==sx-1&&y==sy-1)
return cur.step;
if(map[x][y]=='.')
next.step=cur.step+1;
else
next.step=cur.step;
q.push(next);
map[x][y]='*';
}
}
}
return -1;
}
int main ()
{
int i,x,y,res;
while(scanf("%d%d",&a,&b),a||b)
{
for(i=0;i<a;i++)
scanf("%s",map[i]);
scanf("%d%d",&x,&y);
scanf("%d%d",&sx,&sy);
if(x==sx&&y==sy)
res=0;
else
res=bfs(x-1,y-1);
printf("%d\n",res);
}
return 0;
}