hdu 1026 Ignatius and the Princess I

        hdu 1026 Ignatius and the Princess I

        题目要求的是从(0, 0)到(n-1, m-1)的最小秒数啦, 首先想到的就是广搜啦, 这道题要求输出搜索过的路径哦, 那么就需要一个额外的数组来记录了.

        用turn[][]数组来记录每次转过的方向, 一次广搜结束后, 然后再从终点顺着走回来, 就可完成路径的记录啦.


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

#define MAX 101
#define INF 0xfffffff

struct cell {
    int x, y;
    int step;

    friend bool operator<(cell a, cell b) {
        return a.step > b.step;
    }
};

int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
char map[MAX][MAX];
bool visited[MAX][MAX];
int turn[MAX][MAX]; // 记录每次转向
cell start, end, go, to;
int n, m;
int result;

int bfs() {
    int i;
    int ans = INF;
    priority_queue<cell> Q;

    memset(visited, false, sizeof(visited));
    memset(turn, 0, sizeof(turn));
    start.x = 0, start.y = 0, start.step = 0;
    end.x = n - 1, end.y = m - 1;
    Q.push(start);
    visited[start.x][start.y] = true;

    while (!Q.empty()) {
        go = Q.top();
        Q.pop();

        if (go.x == end.x && go.y == end.y) {
            ans = go.step;
            return ans;
        }

        for (i = 0; i < 4; i++) {
            to.x = go.x + dir[i][0];
            to.y = go.y + dir[i][1];
            to.step = go.step + 1;

            if (to.x >= 0 && to.x < n && to.y >= 0 && to.y < m
                && map[to.x][to.y] != 'X' && !visited[to.x][to.y]) {

                if (map[to.x][to.y] >= '1' && map[to.x][to.y] <= '9') {
                    to.step += map[to.x][to.y] - '0';
                }

                visited[to.x][to.y] = true;
                turn[to.x][to.y] = i;
                Q.push(to);
            }
        }
    }

    return  ans;
}

int printPath(int x,int y) {
    int step, i;

    if(x == 0 && y == 0) {
        step = 1;
        printf("%ds:(%d,%d)->", step, x, y);
        return step;
    }

    i = turn[x][y];
    step = printPath(x - dir[i][0], y - dir[i][1]);
    printf("(%d,%d)\n", x, y);
    if(map[x][y] >= '1' && map[x][y] <= '9') {
        int s;
        s = map[x][y] - '0';
        while(s--) {
            ++step;
            printf("%ds:FIGHT AT (%d,%d)\n", step, x, y);
        }
    }

    // 
    if(step == result) 
       return 0;

    printf("%ds:(%d,%d)->", ++step, x, y);

    return step;
}

int main() {
    int i;

    while (scanf("%d%d", &n, &m) == 2) {
        memset(map, 0, sizeof(map));

        for (i = 0; i < n; i++) {
            scanf("%s", map[i]);
        }

        result = bfs();
        if (result == INF) {
            printf("God please help our poor hero.\nFINISH\n");
        } else {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", result);
            printPath(n - 1, m - 1);
            printf("FINISH\n");
        }
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值