• hdu 1026 Ignatius and the Princess I


     hdu 1026 Ignatius and the Princess I
    //hdu 1026 Ignatius and the Princess I
    //广搜
    
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    
    using namespace std;
    #define N 105
    #define INF 1<<30
    #define eps 1e-5
                        // x            y
    const int dir[2][4] = {-1, 0, 1, 0, 0, -1, 0, 1};
    
    struct QUE
    {
        int x, y, time, step;
    }que[1000000];
    
    int line, row, head, cnt, tail;
    int pre[100000];
    char map[N][N];
    
    bool no_overmap(int x, int y)
    {
        if(x >= 0 && x < line && y >= 0 && y < row)
            return true;
        return false;
    }
    
    void add(int pri, int x, int y, int step, int time)
    {
        pre[++head] = pri;
        que[head].x = x;
        que[head].y = y;
        que[head].step = step;
        que[head].time = time;
    }
    
    bool bfs()
    {
        head = 0;
        tail = 0;
        que[++head].x = que[head].y = que[head].step = que[head].time = 0;
        map[0][0] = 'X';
        while(tail < head)
        {
            tail++;     //出队
            int x = que[tail].x, y = que[tail].y,
                step = que[tail].step;
    
            if(que[tail].time > 0)  //若有怪物,要杀完才能过去,这一步就必须停着
            {                   //将杀怪时间减一秒,所需时间加一秒,放入队列
                add(tail, x, y, step + 1, que[tail].time - 1);
                continue;
            }
            else
            {           //刚从队列里出来的状态若到出口,则放回
                if(x == line - 1 && y == row - 1)
                    return true;
                for(int i = 0; i < 4; ++i)
                {
                    int nx = x + dir[0][i], ny = y + dir[1][i];
                    if(no_overmap(nx, ny) && map[nx][ny] != 'X')
                    {
                        int time;
                        if(map[nx][ny] >= '0' && map[nx][ny] <= '9')
                            time = map[nx][ny] - '0';
                        else
                            time = 0;
                        add(tail, nx, ny, step + 1, time);
    
                        map[nx][ny] = 'X';
                    }
                }
            }
        }
        return false;
    }
    
    void print_road(int now, int x, int y)  //回溯输出
    {
        if(pre[now] != 0)
            print_road(pre[now], que[now].x, que[now].y);
        if(que[now].time > 0)
            printf("%ds:FIGHT AT (%d,%d)\n", cnt++, que[now].x, que[now].y);
        else
            printf("%ds:(%d,%d)->(%d,%d)\n", cnt++, que[now].x, que[now].y, x, y);
    }
    
    int main()
    {
        while(scanf("%d%d", &line, &row) != EOF)
        {
            for(int i = 0; i < line; ++i)
            {
                getchar();
                for(int j = 0; j < row; ++j)
                    map[i][j] = getchar();
            }
            if(bfs())
            {
                printf("It takes %d seconds to reach the target position, let me show you the way.\n", que[tail].step);
                cnt = 1;
                print_road(pre[tail], line - 1, row - 1);
            }
            else
                printf("God please help our poor hero.\n");
            puts("FINISH");
        }
        return 0;
    }


    福建农林大学OJ也有一题和这题差不多

    http://acm.fafu.edu.cn/problem.php?id=1190,就是没有Special Judge

    要按一定顺序才可以,不能用STL的priority_queue  ,而杭电的可以,因为若把节点放进队列

    队列会重新排序,就不会按一定的方向出队了,我这也不是用优先队列做的,可在fafu 一直wa

  • 相关阅读:
    分治法求最大子序列
    6.2 链表 (UVa 11988, 12657)
    6.1 栈和队列 (UVa 210, 514, 442)
    S-Tree (UVa 712) 二叉树
    Equilibrium Mobile (UVa 12166) dfs二叉树
    Patrol Robot (UVa 1600) BFS
    Knight Moves (UVa 439) BFS
    Tree Recovery (UVa 536) 递归遍历二叉树
    Parentheses Balance (Uva 673) 栈
    Self-Assembly (UVa 1572)
  • 原文地址:https://www.cnblogs.com/gabo/p/2594001.html
Copyright © 2020-2023  润新知