hdu1180诡异的楼梯 bfs

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
struct Node
{
    int x, y;
    int step;
    int t;
};
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
    //  freopen("C:\\Users\\Sky\\Desktop\\dabiao.txt","w",stdout);
#endif
    int n, m;
    while (scanf("%d%d", &n, &m) + 1)
    {
        char mp[30][30];
        int vis[30][30] = zero;

        for (int i = 0; i < n; i++)
        {
            scanf("%s", mp[i]);
        }
        Node node;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (mp[i][j] == 'S')
                {
                    node.x = i;
                    node.y = j;
                    node.step = 0;
                    node.t = 0;
                    break;
                }
            }
        }

        queue<Node>Q;
        Q.push(node);
        int T = 0;
        while (!Q.empty())
        {
            node = Q.front();
            Q.pop();
            int x, y;

            // printf("*****************%d\n", T);//可以用来调试哦
            // for (int d = 0; d < n; d++)
            // {
            //     for (int k = 0; k < m; k++)
            //     {
            //         if (d == node.x && k == node.y)
            //             printf("@");
            //         else
            //             printf("%c", mp[d][k]);
            //     }
            //     printf("\n");
            // }

            if (mp[node.x][node.y] == 'T')
                break;
            if (node.t != T)
            {
                T++;
            }
            for (int i = 0; i < 4; i++)
            {
                x = node.x + dir[i][0];
                y = node.y + dir[i][1];

                if (T & 1) //变
                {
                    if (i == 0 || i == 1)
                    {
                        if (mp[x][y] == '|')
                        {
                            x = x + dir[i][0];
                            y = y + dir[i][1];
                        }
                        if (mp[x][y] == '-')
                        {
                            Node addnode;
                            addnode.x = node.x;
                            addnode.y = node.y;
                            addnode.step = node.step;
                            addnode.t = T + 1;
                            Q.push(addnode);
                            continue;
                        }
                    }
                    else
                    {
                        if (mp[x][y] == '|')
                        {
                            Node addnode;
                            addnode.x = node.x;
                            addnode.y = node.y;
                            addnode.step = node.step;
                            addnode.t = T + 1;
                            Q.push(addnode);
                            continue;
                        }
                        if (mp[x][y] == '-')
                        {
                            x = x + dir[i][0];
                            y = y + dir[i][1];
                        }
                    }


                }
                else
                {
                    if (i == 2 || i == 3)
                    {
                        if (mp[x][y] == '|')
                        {
                            x = x + dir[i][0];
                            y = y + dir[i][1];
                        }
                        if (mp[x][y] == '-')
                        {
                            Node addnode;
                            addnode.x = node.x;
                            addnode.y = node.y;
                            addnode.step = node.step;
                            addnode.t = T + 1;
                            Q.push(addnode);
                            continue;
                        }
                    }
                    else
                    {
                        if (mp[x][y] == '|')
                        {
                            Node addnode;
                            addnode.x = node.x;
                            addnode.y = node.y;
                            addnode.step = node.step;
                            addnode.t = T + 1;
                            Q.push(addnode);
                            continue;
                        }
                        if (mp[x][y] == '-')
                        {
                            x = x + dir[i][0];
                            y = y + dir[i][1];
                        }
                    }

                }
                if (x < 0 || y < 0 || x >= n || y >= m || mp[x][y] == '*' || vis[x][y])
                    continue;
                Node addnode;
                addnode.x = x;
                addnode.y = y;
                addnode.step = node.step + 1;
                addnode.t = T + 1;
                vis[x][y] = 1;
                Q.push(addnode);
            }
            
        }
        if (Q.empty()&&mp[node.x][node.y]!='T')
            printf("no way\n");
        else
            printf("%d\n", node.t);

    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值