简单BFS+优先队列。

 

CODE:

 

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

#define SIZE 21
#define INF 0x3f3f3f3f

const int dx[] = {1, -100};
const int dy[] = {00, -11};
char maze[SIZE][SIZE];
int Time[SIZE][SIZE];
int N, M;
int bx, by, ex, ey;
int tS, tP, tT;

struct node
{
    friend bool operator< (const node &a, const node &b)  //优先队列 
    {
        return a.step > b.step;
    }
    int x, y;
    int step;
}p, q;


int check(int r, int c)
{
    if(maze[r][c] != '@' && r >= 0 && c >= 0 && r < N && c < M) 
        return 1;
    return 0;
}

int bfs()
{
    priority_queue<node> Q;
    p.x = bx; p.y = by;
    p.step = 0;
    Q.push(p);
    while(!Q.empty())
    {
        p = Q.top();
        Q.pop();
        if(p.x == ex && p.y == ey)
        {
            if(maze[p.x][p.y] != '@')
                return p.step;
            return -1;
        }
        for(int i = 0; i < 4; i++)
        {
            q = p;
            q.x += dx[i];
            q.y += dy[i];
            if(check(q.x, q.y))
            {
                if(maze[q.x][q.y] == '.' && (q.step+tS) < Time[q.x][q.y])
                {
                    q.step += tS;
                    Q.push(q);
                    Time[q.x][q.y] = q.step;
                }
                if(maze[q.x][q.y] == 'T' && (q.step+tT) < Time[q.x][q.y])
                {
                    q.step += tT;
                    Q.push(q);
                    Time[q.x][q.y] = q.step;
                }
                if(maze[q.x][q.y] == '#' && (q.step+tP) < Time[q.x][q.y])
                {
                    q.step += tP;
                    Q.push(q);
                    Time[q.x][q.y] = q.step;
                }
            }
        }
    }
    return -1;
}


int main()
{
    int i, j;
    int times = 0;
    while(~scanf("%d%d", &N, &M))
    {
        scanf("%d%d%d", &tP, &tS, &tT);
        getchar();
        for(i = 0; i < N; i++)
        {
            for(j = 0; j < M; j++)
            {
                Time[i][j] = INF;
                scanf("%c", &maze[i][j]);
            }
            getchar();
        }
        scanf("%d%d%d%d", &bx, &by, &ex, &ey);
        printf("Case %d: ", ++times);
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}

 

CODE2:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;

const int SIZE = 21;
const int dx[] = {1, -100};
const int dy[] = {00, -11};

char maze[SIZE][SIZE];
int v[SIZE][SIZE];
int bx, by, ex, ey;
int tT, tS, tP;
int n, m;

struct node
{
    friend bool operator< (const node &a, const node &b)  //优先队列 
    {
        return a.step > b.step;
    }
    int x, y, step;
}p, q;

void init()
{
    memset(v, 0sizeof(v));
    tT = tS = tP = 0;
}

int check(int x, int y)
{
    if(x >= 0 && x < n && y >= 0 && y < m && maze[x][y]!= '@'return 1;
        return 0;
}

int bfs()
{
    priority_queue<node> Q;
    if(bx == ex && by == ey)    return 0;
    p.x = bx; p.y = by; p.step = 0;
    v[bx][by] = 1;
    Q.push(p);
    while(!Q.empty())
    {
        p = Q.top();
        Q.pop();
        if(p.x == ex && p.y == ey)
        {
            if(maze[p.x][p.y] != '@')
                return p.step;
            return -1;
        }
        for(int i = 0; i < 4; i++)
        {
            q = p;
            q.x += dx[i];
            q.y += dy[i];
            if(check(q.x, q.y) && !v[q.x][q.y])
            {
                if(maze[q.x][q.y] == 'T')
                {
                    q.step += tT;
                    v[q.x][q.y] = 1;
                    Q.push(q);
                }
                if(maze[q.x][q.y] == '.')
                {
                    v[q.x][q.y] = 1;
                    q.step += tS;
                    Q.push(q);
                }
                if(maze[q.x][q.y] == '#')
                {
                    v[q.x][q.y] = 1;
                    q.step += tP;
                    Q.push(q);
                }
            }
        }
    }
    return -1;
}

int main()
{
    int times = 0;
    while(~scanf("%d%d", &n, &m))
    {
        init();
        scanf("%d%d%d", &tP, &tS, &tT);
        for(int i = 0; i < n; i++)
            scanf("%s", maze[i]);
        scanf("%d%d%d%d", &bx, &by, &ex, &ey);
        printf("Case %d: ", ++times);
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}

 

 

posted on 2012-09-15 14:43  有间博客  阅读(223)  评论(0编辑  收藏  举报