Dragon Maze -- BFS很好的应用

题目连接:http://acdream.info/problem?pid=1191


code:

<span style="font-size:12px;">/*
* this code is made by Edward_Wong
* Problem: 1191
* Verdict: Accepted
* Submission Date: 2014-09-22 18:48:56
* Time: 96MS
* Memory: 9580KB
*/
#include <cstdio>
#include <algorithm>
#include <functional>
#include <stack>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <map>
#include <cmath>
#define LL long long
#define lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
const int MAXN = 1000 + 5;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
struct POINT
{
    int x, y, eng, steps;
    POINT()
    {
        x = y = eng = steps = 0;
    }
    POINT(int xx, int yy): x(xx), y(yy), eng(0), steps(0){}
};
struct cmp
{
    bool operator() (const POINT &a, const POINT &b)
    {
        if (a.steps == b.steps) return a.eng < b.eng;
        else return a.steps > b.steps;
    }
};
priority_queue<POINT, vector<POINT>, cmp>pqu;
int mp[MAXN][MAXN], vis[MAXN][MAXN], row, col;
const int dir[][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
POINT ed;
POINT BFS(POINT st)
{
    while (!pqu.empty())
        pqu.pop();
    memset(vis, 0, sizeof vis);
    pqu.push(st);
    while (!pqu.empty())
    {
        POINT cur = pqu.top(); pqu.pop();
        for (int i = 0; i < 4; i++)
        {
            int xx = cur.x + dir[i][0], yy = cur.y + dir[i][1];
            if (!vis[xx][yy] && mp[xx][yy] != -1 && xx >= 0 && xx < row && yy >= 0 && yy < col)
            {
                vis[xx][yy] = 1;
                POINT tmp(xx, yy);
                tmp.eng = cur.eng + mp[xx][yy]; tmp.steps = cur.steps + 1;
                if (xx == ed.x && yy == ed.y)
                    return tmp;
                pqu.push(tmp);
            }
        }
    }
    POINT fal;
    return fal;
}
int main()
{
    //freopen("input.txt", "r", stdin);
    int T, i, j, cases = 0;
    scanf("%d", &T);
    while (T--)
    {
        POINT st;
        scanf("%d%d", &row, &col);
        scanf("%d%d%d%d", &st.x, &st.y, &ed.x, &ed.y);
        for (i = 0; i < row; i++)
            for (j = 0; j < col; j++) scanf("%d", &mp[i][j]);
        st.eng = mp[st.x][st.y];
        POINT ans = BFS(st);
        printf("Case #%d: ", ++cases);
        if (ans.steps != 0)
            printf("%d\n", ans.eng);
        else
            puts("Mission Impossible.");
    }
    return 0;
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值