HDU 1026 Ignatius and the Princess I BFS

本文介绍了一种使用广度优先搜索(BFS)解决迷宫问题的方法,具体为从起点(1,1)到终点(n,m)的最短路径寻找过程,并考虑了特定方格停留时间的要求。文章详细展示了算法实现细节,包括如何处理路径记录及输出等复杂问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:从 (1,1) (n,m) 最少要走多少秒?(如果方格上有数字,意思就是要停留多少秒?)
思路:BFS,对于方格上有数字的情况,把数字减1,然后再次加入队列,对于记录路径,我是 A[i][j] 代表到达 (i,j) 的前一个点,输出的时候从 A[n][m] 往前面找即可。
坑点:输出路径,烦的一匹。然而一遍过还是感觉萌萌哒。

http://acm.hdu.edu.cn/showproblem.php?pid=1026

/*********************************************
    Problem : HDU 1026
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

struct Node {
    int x,y,t,nt;
    int fromx,formy;
    Node() {}
    Node(int _x,int _y,int _t,int _nt,int _fromx,int _fromy) {x = _x;y = _y;t = _t;nt = _nt;fromx = _fromx ; formy = _fromy;}
}t[10005];

struct Point {
    int x,y;
    Point() {}
    Point(int _x,int _y) {x = _x;y = _y;}
}A[105][105];

Point root[225];

int T,n,m,k;

char Map[105][105];
char BMap[105][105];

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,1,-1};
int second ;

queue<Node>q;

bool BFS() {
    while(!q.empty())q.pop();
    q.push(Node(1,1,0,0,0,0));
    while(!q.empty()) {
        Node tmp = q.front();
        q.pop();
        if(tmp.x == n && tmp.y == m ) {
            second = tmp.t + tmp.nt;
            return true;
        }
        if(tmp.nt) {
            q.push(Node(tmp.x,tmp.y,tmp.t+1,tmp.nt-1,tmp.x,tmp.y)) ;
            continue;
        }
        rep(i,1,4) {
            int tmpx = tmp.x + fx[i]; int tmpy = tmp.y + fy[i];
            if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m) {
                if(Map[tmpx][tmpy] == '.')  {
                    q.push(Node(tmpx,tmpy,tmp.t+1,0,tmp.x,tmp.y));
                    A[tmpx][tmpy] = Point(tmp.x,tmp.y);
                } 
                else if(Map[tmpx][tmpy] == 'X') {

                }
                else {
                    q.push(Node(tmpx,tmpy,tmp.t+1,Map[tmpx][tmpy]-'0',tmp.x,tmp.y));
                    A[tmpx][tmpy] = Point(tmp.x,tmp.y);
                }
                Map[tmpx][tmpy] = 'X';
            }
        }
    }
    return false;
}

void input() {
    rep(i,1,n) scanf("%s",Map[i]+1) ;
    rep(i,1,n) rep(j,1,m) {
        BMap[i][j] = Map[i][j];
    }
}

void solve() {
    if(BFS()) {
        int tmpx,tmpy,x0,y0;
        int tot = 1;
        tmpx = n ; tmpy = m ; 
        root[tot++] = Point(tmpx,tmpy);
        while(tmpx != 1 || tmpy != 1) {
            x0 = A[tmpx][tmpy].x ; y0 = A[tmpx][tmpy].y;
            tmpx = x0 ; tmpy = y0;
            root[tot++] = Point(tmpx,tmpy);
        }
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",second);
        second = 1;
        rrep(i,1,tot-1) {
            if(i == 1) {
                while(BMap[root[i].x][root[i].y] > '0') {
                    printf("%ds:FIGHT AT (%d,%d)\n",second++,root[i].x-1,root[i].y-1);
                    BMap[root[i].x][root[i].y] -- ;
                }
                break;
            }
            if(BMap[root[i].x][root[i].y] != '.') {
                while(BMap[root[i].x][root[i].y] > '0') {
                    printf("%ds:FIGHT AT (%d,%d)\n",second++,root[i].x-1,root[i].y-1);
                    BMap[root[i].x][root[i].y] -- ;
                }
            }
            printf("%ds:(%d,%d)->(%d,%d)\n",second++,root[i].x-1,root[i].y-1,root[i-1].x-1,root[i-1].y-1);
        }
    }
    else puts("God please help our poor hero.");
    puts("FINISH");
}

int main(void) {
    //freopen("a.in","r",stdin);
    //scanf("%d",&T); while(T--) {
    while(~scanf("%d %d",&n,&m)) {
    //while(scanf("%d",&n),n) {
        input();
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值