HDU 1026(bfs)

来源:互联网 发布:电视软件破解论坛 编辑:程序博客网 时间:2024/05/17 03:31

题意:如题。

#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