hdu 1180 诡异的楼梯 楼梯可以变方向的搜索题 (三)

2014-11-24 01:35:48 · 作者: · 浏览: 6
态为|
{
if(temp.step%2==0)//如果已经走了step步 为偶数步 则状态不变
{
if(can(xx+dir[i][0],yy+dir[i][1]))//方向不变穿越楼梯看是否能走
{
q.x=xx+dir[i][0];
q.y=yy+dir[i][1];
vis[q.x][q.y]=1;
if((i==1||i==0))//0,1对应的是竖着走 因此直接走 所以加1
{

q.step=temp.step+1;
que.push(q);
}
else//2 3 对应横着走 由于是状态不变 所以要等到它变为-后再走 所以加2
{
q.step=temp.step+2;
que.push(q);
}
}

}
else
{
if(can(xx+dir[i][0],yy+dir[i][1]))
{
q.x=xx+dir[i][0];
q.y=yy+dir[i][1];
vis[q.x][q.y]=1;
if((i==1||i==0))
{

q.step=temp.step+2;
que.push(q);
}
else
{
q.step=temp.step+1;
que.push(q);
}
}
}
}
else if(map[xx][yy]=='-')
{
if(temp.step%2==0)
{
if(can(xx+dir[i][0],yy+dir[i][1]))
{
q.x=xx+dir[i][0];
q.y=yy+dir[i][1];
vis[q.x][q.y]=1;
if((i==1||i==0))
{

q.step=temp.step+2;
que.push(q);
}
else
{
q.step=temp.step+1;
que.push(q);
}
}

}
else
{
if(can(xx+dir[i][0],yy+dir[i][1]))
{
q.x=xx+dir[i][0];
q.y=yy+dir[i][1];
vis[q.x][q.y]=1;
if((i==1||i==0))
{

q.step=temp.step+1;
que.push(q);
}
else
{
q.step=temp.step+2;
que.push(q);
}
}
}
}
else
{
vis[xx][yy]=1;
/*注意楼梯用过了1遍之后还可以再用 因为用来横着走之后还可以竖着走 所以不标记楼梯是否走过*/
q.x=xx;
q.y=yy;
q.step=temp.step+1;
que.push(q);
}
}
}
}
}
int main()
{
int i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=0;i {
scanf("%s",map[i]);
for(j=0;j {
if(map[i][j]=='S') {sx=i;sy=j;}
else if(map[i][j]=='T') {ex=i;ey=j;}
}
}
int ans=0; ans=BFS();
printf("%d\n",ans);
}
return 0;
}