诡异的楼梯

HDU1180:Link Hrer

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

struct Point
{
    int x,y,step;
    friend bool operator < (const Point a,const Point b)
    {
        return a.step > b.step;
    }
};
int dir[5][3] = {{1,0},{0,1},{-1,0},{0,-1}};
int n,m,s,e;
char map[22][22];
void Manage()
{
    for(int i = 0;i <= n+1; i++)
    {
        map[i][0] = '*';
        map[i][m+1] = '*';
    }
    for(int j = 0;j <= m+1; j++)
    {
        map[0][j] = '*';
        map[n+1][j] = '*';
    }
}
bool IsNeedPlus(Point temp,int i)
{
    if(map[temp.x][temp.y] == '-'){
      if(i % 2 == 0){
          if(temp.step % 2 == 0)
            return false;
        else
          return true;
      }else
      {
           if(temp.step % 2 == 0)
             return true;
         else
           return false;
        }
    }else
    {
        if(i % 2 == 0){
          if(temp.step % 2 == 0)
            return true;
          else
            return false;
        }else
        {
            if(temp.step % 2 == 0)
              return false;
            else
              return true;
        }
    }
}
void BFS(int u,int v)
{
    priority_queue <Point>Q;
    Point temp,q;
    q.x= u; q.y = v; q.step = 0;
    Q.push(q);
    while(!Q.empty())
    {
        q = Q.top(); Q.pop();
        if(q.x == s&&q.y == e)
        {
            printf("%d\n",q.step);
            return ;
        }
        q.step ++;
        for(int i = 0;i < 4;i++)
        {
            temp.x = q.x + dir[i][0];
            temp.y = q.y + dir[i][1];
            temp.step = q.step;
            if(temp.x != '*')
            {
                if(map[temp.x][temp.y] == '.'||map[temp.x][temp.y] == 'T')
                {
                    map[temp.x][temp.y] = '*';
                    Q.push(temp);
                }else if(map[temp.x][temp.y]=='|'||map[temp.x][temp.y]=='-')
                {
                    if(!IsNeedPlus(temp,i)){
                      temp.x += dir[i][0];
                      temp.y += dir[i][1];
                      if(map[temp.x][temp.y] != '*')
                      {
                           map[temp.x][temp.y] = '*';
                           Q.push(temp);
                        }
                    }else
                    {
                        Q.push(q);
                    }
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
       for(int i = 1;i <= n;i++)
         scanf("%s",map[i]+1);
       Manage();
       int u,v;
       for(int i = 1;i <= n; i++)
            for(int j = 1;j <= m; j++)
            {
                if(map[i][j] == 'S')
                {
                    u = i;
                    v = j;
               }
               if(map[i][j] == 'T')
               {
                   s = i;
                   e = j;
               }
             }
         BFS(u,v);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值