HDU 1180 诡异的楼梯

题意:
(中文)
思路:
由于每过1分钟,这个楼梯的通向就会变一次,要走楼梯时,判断一下已经走过的步数是否是偶数,如果是偶数,那么楼梯则没有变,否则,变换楼梯方向。判断一下当前是否可以走这个楼梯,可以走的话,步数就是当前步数+1,否则就是+2(等一分钟再走)。
但是,这里面如果用广搜的话,就会出现这样一个问题:第一个到达的那个点其实并不是最优的步数。例如:
3 4
T…
*-*S
*.|.
如果是这样的话,答案就会是5,但是正确答案却是4。
因为在优先搜索步数的时候,(0,1)这个点已经被标记成不可经过的了(因为下面走楼梯的那条路搜索的时候先到达了(0,1)这个点)。
所以需要有这样一个规则,如果此个地方走过了,只要我到达此个地方的步数少,那么我就还可以走这个地方。
Code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>

#define TEST

#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
    #define See(a) cout << #a << " = " << a << endl;
    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
    #define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';} cout << endl;
    #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee)}
#else
    #define See(a)
    #define See2(a, b)
    #define debug(a, s, e)
    #define debug2(a, s, e, ss, ee)
#endif // TEST

const int MAX = 2e9;
const int MIN = -2e9;
const double eps = 1e-8;
const double PI = acos(-1.0);

using namespace std;

const int N = 25;

int mv[4][2] = {
    {1, 0},
    {0, 1},
    {-1, 0},
    {0, -1}
};
int n, m;
char ch[N][N];
bool v[N][N];
int f[N][N];
int sx, sy, ex, ey;

struct Node
{
    int x;
    int y;
    int tep;
    friend bool operator < (Node a, Node b)
    {
        return a.tep > b.tep;
    }
    Node(){}
    Node(int x, int y, int tep) : x(x), y(y), tep(tep) {}
}st, to;

char getC(char c, int tep)//变换楼梯
{
    if(c == '|' || c == '-')
    {
        if(tep & 1)
        {
            c = (c == '|' ? '-' : '|');
        }
    }
    return c;
}

bool isOK(int x, int y, int tep)//判断下一个是否可以走,如果走过,那就是步数少于以前步数的情况,可以再次通过。
{
    if(x < 0 || y < 0 || x >= n || y >= m)
    {
        return false;
    }
    if(v[x][y])
    {
        if(f[x][y] > tep)
        {
            return true;
        }
        return false;
    }
    return true;

}

int getTep(char c, int i)//判断下一步应该多出几分钟(1分种还是等一会2分种)
{
    if(i & 1)
    {
        if(c == '-')
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    else
    {
        if(c == '|')
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
}

int bfs()
{
    st = Node(sx, sy, 0);
    v[sx][sy] = true;
    f[sx][sy] = 0;
    priority_queue<Node> Q;
    Q.push(st);
    while(!Q.empty())
    {
        st = Q.top(); Q.pop();
//        printf("st.xt = %d st.y = %d st.tep = %d\n", st.x, st.y, st.tep);
        if(ch[st.x][st.y] == 'T')
        {
            return st.tep;
        }
        for(int i = 0; i < 4; ++i)
        {
            int x = st.x + mv[i][0];
            int y = st.y + mv[i][1];
            if(isOK(x, y, st.tep + 1) && ch[x][y] != '*')
            {
                if(ch[x][y] != '|' && ch[x][y] != '-')
                {
                    v[x][y] = true;
                    f[x][y] = st.tep + 1;
                    Q.push(Node(x, y, st.tep + 1));
                }
                else
                {
                    char c = getC(ch[x][y], st.tep);
                    int tx = x, ty = y;
                    if(i & 1)
                    {
                        ty += mv[i][1];
                    }
                    else
                    {
                        tx += mv[i][0];
                    }
                    int tep = getTep(c, i);
                    if(isOK(tx, ty, st.tep + tep) && ch[tx][ty] != '*')
                    {
                        v[tx][ty] = true;
                        f[tx][ty] = st.tep + tep;
                        Q.push(Node(tx, ty, f[tx][ty]));
                    }
                }
            }

        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        Mt(v, false);
        Mt(f, -1);
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", ch[i]);
            for(int k = 0; ch[i][k]; ++k)
            {
                if(ch[i][k] == 'S')
                {
                    sx = i;
                    sy = k;
                }
            }
        }
        printf("%d\n", bfs());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值