hdoj 4198 Quick out of the Harbour

3 篇文章 0 订阅

类型:bfs + 优先队列

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4198

来源:BAPC 2011

思路:扩展完当前节点后,将"路径"小的节点出队,进行扩展。

!!!S点可能在边上

// hdoj 4198 Quick out of the Harbour
// wa ac 125MS 764K
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int MAXN = 510;
const int MAXM = 0;
const int hash_size = 25000002;
const int INF = 0x7f7f7f7f;

bool vis[MAXN][MAXN];
int test, h, w, d, sx, sy;
int dirx[4] = {-1, 0, 1, 0}, diry[4] = {0, 1, 0, -1};
char str[MAXN][MAXN];
struct node {
    int x, y, step;
};
bool operator > (const node &a, const node &b) {
    return a.step > b.step;
}
bool operator < (const node &a, const node &b) {
    return a.step < b.step;
}
priority_queue<node, vector<node>, greater<node> > q;

void init() {
    int i, j;
    scanf("%d %d %d", &h, &w, &d);
    CLR(vis, false);
    while(!q.empty())
        q.pop();
    FORE(i, 1, h) {
        scanf("%s", str[i] + 1);
        FORE(j, 1, w)
            if(str[i][j] == 'S')
                sx = i, sy = j;
    }
}

int bfs() {
    int i;
    node a, b;
    a.x = sx, a.y = sy, a.step = 1;
    q.push(a);
    //!!!
    if(a.x == 1 || a.x == h || a.y == 1 || a.y == w)
        return a.step;
    while(!q.empty()) {
        a = q.top();
        q.pop();
        FORE(i, 0, 3) {
            int tmp_x = a.x + dirx[i], tmp_y = a.y + diry[i];
            if(tmp_x < 1 || tmp_x > h || tmp_y < 1 || tmp_y > w || vis[tmp_x][tmp_y] || str[tmp_x][tmp_y] == '#')
                continue;
            vis[tmp_x][tmp_y] = true;
            b.x = tmp_x, b.y = tmp_y;
            if(str[tmp_x][tmp_y] == '.')
                b.step = a.step + 1;
            else
                b.step = a.step + 1 + d;
            if(b.x == 1 || b.x == h || b.y == 1 || b.y == w)
                return b.step;
            q.push(b);
        }
    }
}

int main() {
    scanf("%d", &test);
    while(test--) {
        init();
        printf("%d\n", bfs());
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值