时间数组标记是否将遇到的位置进队列即可,应该用优先队列也是可以的,不过麻烦。
#include
#include
#include
using namespace std;
struct point
{
int x,y,step;
}s,e;
queue q;
int n,m,tim[25][25];
char map[25][25];
int h[4][2]={1,0,-1,0,0,1,0,-1};
int BFS()
{
while(!q.empty()) q.pop();
int i,j,xx,yy,shu; point t,tt;
q.push(s);tim[s.x][s.y]=0;
while(!q.empty())
{
t=q.front(); q.pop();
for(i=0;i<4;i++)
{
xx=t.x+h[i][0]; yy=t.y+h[i][1];
if(xx>=0&&xx=0&&yy=0&&xx=0&&yy1) tt.step+=1;
if(tt.step
=0&&xx=0&&yy1||t.step%2==0&&i<=1) tt.step+=1;
if(tt.step>n>>m)
{
for(i=0;i>map[i][j];
if(map[i][j]=='S')
{
s.x=i; s.y=j; s.step=0; tim[i][j]=0;
}
else if(map[i][j]=='T')
{
e.x=i; e.y=j;
}
}
memset(tim,0x33,sizeof(tim));
cout<