杭电1180 诡异的楼梯

BFS

题目大意是说:给你一张地图,给你起点,终点,问你从起点走到终点的最少时间(一格是一时间单位),简单来说就是BFS 求最短路径问

题;

这个题需要注意的是:如果走到楼梯上,且当时楼梯是不可走状态,可以原地呆在那(当时没看见,WA五六遍,,,),等楼梯可走时再走



可以直接写BFS,也可以优先队列;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;

struct point
{
    int x,y,step;
};

point bg;
char mp[25][25];
int mark[25][25];
int dir[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };

int bfs()
{
    queue<point> ss;
    ss.push(bg);
    mark[bg.x][bg.y]=0;
    while(ss.size())
    {
        point f=ss.front();
        ss.pop();
        for(int i=0; i<4; i++)
        {
            point g;
            g.x = f.x + dir[i][0];
            g.y = f.y + dir[i][1];
            g.step = f.step + 1;
            if(mp[g.x][g.y] != '*' && mark[g.x][g.y] > g.step)
            {
                if( mp[g.x][g.y] == '-')
                {
                    if((f.step&1)&&(i>=2))//两个if判断楼梯当前状态是否可走,若不可走,将原地址加入队列,时间加一;
                    {
                        g.x = f.x, g.y = f.y;
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                        continue;
                    }
                    if(((f.step&1)==0)&&(i<2))
                    {
                        g.x = f.x, g.y = f.y;
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                        continue;
                    }
                    g.x = g.x + dir[i][0] , g.y = g.y + dir[i][1];
                    if( mark[g.x][g.y] <= g.step )  continue;
                    if(mp[g.x][g.y]=='.')//判断楼梯后面是否可走;
                    {
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                    }
                    else if(mp[g.x][g.y]=='T')
                        return g.step;
                }
                else if(mp[g.x][g.y]=='|')
                {
                    if((f.step&1)&& i<2)
                    {
                        g.x = f.x, g.y = f.y;
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                        continue;
                    }
                    if( ((f.step&1)==0) && i>= 2)
                    {
                        g.x = f.x, g.y = f.y;
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                        continue;
                    }
                    g.x = g.x + dir[i][0] ,g.y = g.y + dir[i][1];
                    if( mark[g.x][g.y]<=g.step ) continue;
                    if(mp[g.x][g.y]=='.')
                    {
                        mark[g.x][g.y] = g.step;
                        ss.push(g);
                    }
                    else if(mp[g.x][g.y]=='T')
                        return g.step;
                }
                else if(mp[g.x][g.y]=='.')
                {
                    mark[g.x][g.y] = g.step;
                    ss.push(g);
                }
                else if(mp[g.x][g.y]=='T')
                    return g.step;
            }
        }
    }
    return 0;
}

int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(int i=0; i<22; i++)
            for(int j=0; j<22; j++)
                mp[i][j]='*', mark[i][j]=0x3f3f3f3f;
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf(" %c",&mp[i][j]);
                if( mp[i][j]=='S' )
                    mp[i][j]='.',  bg.x=i,  bg.y=j,  bg.step=0;
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值