Hdu 2425 Hiking Trip

 

简单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;
}

 

 

转载于:https://www.cnblogs.com/g0feng/archive/2012/09/15/2686499.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值