HDU 1026(bfs)

题意:如题。

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;

int N, M;
const int step[4][3] = { {0, 1, 0}, {1, -1, 0}, {2, 0, 1}, {3, 0, -1} };
int time[101][101];
int LD[101][101]; // last direction 上一个方向
char maze[102][102];
struct Point
{
    int x, y, s, ld;
    Point() : s(0), ld(-1) {}
    Point(int _x, int _y, int _s, int _ld) : x(_x), y(_y), s(_s), ld(_ld) {}
    bool operator <(const Point& rhs) const
    {
        return s > rhs.s;
    }
    bool operator ==(const Point& rhs) const
    {
        return x == rhs.x && y == rhs.y;
    }
    Point operator +(const int S[]) const
    {
        return Point(x + S[1], y + S[2], s + 1, S[0]);
    }
    Point operator -(const int S[]) const
    {
        return Point(x - S[1], y - S[2], 0, -1);
    }
    bool legal()
    {
        if (maze[x][y] == 'X')
            return false;
        else
        {
            if (maze[x][y] != '.')
                s += maze[x][y] - '0';
            if (time[x][y] == 0 || time[x][y] > s)
            {
                time[x][y] = s;
                LD[x][y] = ld;
                return true;
            }
            else return false;
        }
    }
    friend ostream& operator <<(ostream& os, const Point& rhs)
    {
        return os << "(" << rhs.x-1 << "," << rhs.y-1 << ")";
    }

} start(1, 1, 0, -1), end;

void backtrack(Point& p)
{
    if (p == start)
        return;
    Point n = p - step[LD[p.x][p.y]];
    backtrack(n);
    int t = 0;
    cout << (time[p.x][p.y] - t) << "s:" << n << "->" << p << endl;
    if (maze[p.x][p.y] != '.')
    {
        t = maze[p.x][p.y] - '0';
        while (t--)
            cout << (time[p.x][p.y] - t) <<  "s:FIGHT AT " << p << endl;
    }
}

void backtrack()
{
    stack<Point> s;
    Point p = end, first, second;
    s.push(p);
    int t;
    while (!(p == start))
    {
        p = p - step[LD[p.x][p.y]];
        s.push(p);
    }
    first = s.top(), s.pop();
    while (!s.empty())
    {
        second = s.top(), s.pop();
        if (maze[second.x][second.y] != '.')
            t = maze[second.x][second.y] - '0';
        else t = 0;
        //printf("%ds:(%d,%d)->(%d,%d)\n", time[second.x][second.y] - t, first.x-1, first.y-1, second.x-1, second.y-1);
        cout << (time[second.x][second.y] - t) << "s:" << first << "->" << second << endl;
        while (t--)
            //printf("%ds:(%d,%d)->(%d,%d)\n", time[second.x][second.y] - t, first.x-1, first.y-1, second.x-1, second.y-1);
            cout << (time[second.x][second.y] - t) <<  "s:FIGHT AT " << second << endl;
        first = second;
    }
}

void BFS()
{
    time[1][1] = -1;
    priority_queue<Point> q;
    q.push(start);
    Point p, n;
    while (!q.empty())
    {
        p = q.top(), q.pop();
        for (int i = 0; i < 4; ++i)
        {
            n = p + step[i];
            if (!n.legal())
                continue;
            if (n == end)
            {
                while (!q.empty())
                    q.pop();
                //printf("It takes %d seconds to reach the target position, let me show you the way.\n", n.s);
                cout << "It takes " << n.s << " seconds to reach the target position, let me show you the way." << endl;
                backtrack();
                 //backtrack(n);
                return;
            }
            q.push(n);
        }
    }
    cout << "God please help our poor hero." << endl;
    //printf("God please help our poor hero.\n");
}

int main()
{
    memset(maze[0], 'X', sizeof (maze[0]));
    while (scanf("%d%d", &N, &M) != EOF)
    {
        end.x = N, end.y = M;
        memset(time, 0, sizeof (time));
        memset(LD, -1, sizeof (LD));
        getchar();
        for (int i = 1; i <= N; ++i)
        {
            gets(&maze[i][1]);
            maze[i][0] = maze[i][M+1] = 'X';
        }
        memset(maze[N+1], 'X', sizeof (maze[N+1]));
        BFS();
        cout << "FINISH" << endl;
        //printf("FINISH\n");
    }
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值