hdu1180诡异的楼梯【优先队列+广搜】

好久没写博客,主要还是因为周末的考试和昨天生态学实验,忧伤的周末终于过去,更加的忧伤的日子已经来临orz

————写在前面的废话

这个题本来第一眼看是中文的,十分兴奋,结果……到底还是AC率20+%的题啊,理解错一个地方,有楼梯的地方不是只能顺着走,只是顺着走可以省时间1分钟,能不能最后也省时间不一定……挺简洁优雅的广搜

/******
hdu1180
2015.12.7
0MS 1432K 2625 B G++
******/
#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;


int stair[22][22];


struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
};
int map[22][22];
int n,m;
int dir[4][2]={1,0, 0,-1, -1,0, 0,1};
int x_s,y_s;
int x_e,y_e;


int judge(int x,int y)
{
	if(x<0 || x>=n || y<0 || y>=m)	return 1;
	if(map[x][y]==1)				return 1;
	if(map[x][y]==2)	return 2;
	return 0;
}
int judge2(int step,int x2,int y2,int k)
{
	if((step+stair[x2][y2])%2!=0 && (k==0 || k==2))	return 1;
	if((step+stair[x2][y2])%2==0 && (k==1 || k==3))	return 1;
	return 0;
}
int BFS()
{
    priority_queue<node>q;
    node cur,next;
    int temp;
    int t_x,t_y;
    cur.x=x_s;
    cur.y=y_s;
    cur.step=0;
    map[cur.x][cur.y]=1;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(cur.x==x_e&&cur.y==y_e) return cur.step;
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.step=cur.step;
            temp=judge(next.x,next.y);
            if(temp==1) continue;
            if(!temp)
            {
                next.step++;
                map[next.x][next.y]=1;
                q.push(next);
            }
            else
            {
                t_x=next.x+dir[i][0];
                t_y=next.y+dir[i][1];
                if(judge(t_x,t_y)) continue;//就算有楼梯,跳过去是墙也不能走
                if(judge2(next.step,next.x,next.y,i))//楼梯方向是顺着的
                {
                    next.x=t_x;
                    next.y=t_y;
                    next.step++;
                }
                else//楼梯的方向是逆着的 但是很幸运接着走可以
                {
                    next.x=t_x;
                    next.y=t_y;
                    next.step+=2;
                }
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
	//freopen("cin.txt","r",stdin);
	int i,l;
	int ans;
	char str[25];


	while(scanf("%d%d",&n,&m)!=-1)
	{
		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='S')		{x_s=i;y_s=l;map[i][l]=0;}
				else if(str[l]=='T'){x_e=i;y_e=l;map[i][l]=0;}
				else if(str[l]=='|'){stair[i][l]=1;map[i][l]=2;}
				else if(str[l]=='-'){stair[i][l]=0;map[i][l]=2;}
				else if(str[l]=='*')map[i][l]=1;
				else if(str[l]=='.')map[i][l]=0;
			}
		}


		int ans=BFS();
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值