您的位置:首页 > 其它

hdu 1180 诡异的楼梯

2014-04-09 17:42 357 查看
hdu   1180   诡异的楼梯            题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180

                                 


题目分析:题目描述有点不大清晰,需要补充一点,虽然不能在楼梯上停留,但走廊还是可以停留的,比如在楼梯前等一分钟是可以的。其它的都跟普通BFS一样,楼梯需要加判断,判断当前能否直接通过眼前的楼梯,如果不能就等一分钟。需要注意的是楼梯有可能出现在出口前,从楼梯出来就是出口。

code:

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
char map[30][30];
int m,n,ex,ey,dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node
{
int x,y,step;
};
bool judge(node no)
{
int x=no.x,y=no.y;
return x>=0&&y>=0&&x<m&&y<n&&map[x][y]!='*';
}
bool able(node f,node n)
{
int sx=f.x,sy=f.y,ex=n.x,ey=n.y;
int temp=(sx==ex?1:0);
temp+=map[n.x][n.y]=='-'?1:0;
temp+=f.step%2?1:0;
return !(temp%2);
}
int BFS(int x,int y)
{
queue<node>q;
node first,next;
first.x=x;
first.y=y;
first.step=0;
map[first.x][first.y]='*';
q.push(first);
while(!q.empty())
{
first=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=first.x+dir[i][0];
next.y=first.y+dir[i][1];
if(map[next.x][next.y]=='|'||map[next.x][next.y]=='-')
{
if(!able(first,next))
{
next.step=first.step+2;
}
else next.step=first.step+1;
next.x+=dir[i][0];
next.y+=dir[i][1];
}
else next.step=first.step+1;
if(next.x==ex&&next.y==ey)return next.step;
if(judge(next))
{
//printf("in x==%d y==%d step==%d\n",next.x,next.y,next.step);
q.push(next);
map[next.x][next.y]='*';
}
}
}
return -1;
}
int main()
{
int i,j,sx,sy;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
if(map[i][j]=='S')sx=i,sy=j;
if(map[i][j]=='T')ex=i,ey=j;
}
}
printf("%d\n",BFS(sx,sy));
}
return 0;
}


PS:该去找剪枝学了…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BFS