poj2312 Battle city (优先队列)

#include

#include
#include
using namespace std;
int dir[4][2]={-1,0,  1,0,  0,1,   0,-1};
char map[1000][1000];
int visit[1000][1000];
int n,m;
struct point 
{
int x,y, time ;
friend bool operator<(point a,point b)
{
return a. time >b. time ;
}
};
int judge( int x, int y)
{
if (x<0||y<0||x>=n||y>=m)
return 0;
if (map[x][y]== 'S' ||map[x][y]== 'R' ||visit[x][y])
return 0;
return 1;
}
int bfs( int x0, int y0)
{
int x,y,i;
point in,out;
priority_queueq;
in.x=x0; 
in.y=y0;
in. time =0;
q.push(in);
while (!q.empty())
{
out=q.top();
q.pop();
if (map[out.x][out.y]== 'T' )
{
printf ( "%d\n" ,out. time );
return 1;
}
for (i=0;i<4;i++)
{
x=in.x =out.x+dir[i][0];
y=in.y=out.y+dir[i][1];
if (judge(x,y)==0)
continue ;
in. time =out. time +1;
if (map[x][y]== 'B' )
in. time ++;
visit[x][y]=1;
q.push(in);
}
}
return 0;
}
int main()
{
int i,j,k,h;
while ( scanf ( "%d%d" ,&n,&m),n||m)
{
for (i=0;i
scanf ( "%s" ,map[i]);
for (i=0;i
for (j=0;j
{
if (map[i][j]== 'Y' )
{
k=i;
h=j;
break ;
}
}
memset (visit,0, sizeof (visit));
visit[k][h]=1;
if (!bfs(k,h))
printf ( "-1\n" );
}
return 0;
}

你可能感兴趣的:(优先队列)