Dragon Maze -- BFS很好的应用

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


code:

/*
* this code is made by Edward_Wong
* Problem: 1191
* Verdict: Accepted
* Submission Date: 2014-09-22 18:48:56
* Time: 96MS
* Memory: 9580KB
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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 pii;
typedef vector::iterator viti;
typedef vector::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, 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;
}


你可能感兴趣的:(Dragon Maze -- BFS很好的应用)