zoj 3103 Cliff Climbing 优先队列+BFS

题目链接:

3103


题意:
一块N X M 的墙壁,求从S点出发 到T点的最短时间  每次只能爬一步,且只能左右脚交替爬行,墙上每个方块中的数字标明方块的"光滑
等级",标有数字t 的方块将花费他t 个单位时间安全地将他的脚踏在上面。

题解:
队列结构体中有4个变量(x,y,步数,固定的脚(0左脚,1右脚))。将所有s点的左脚开始和右脚开始 加入队列
标记数组要有三维    表示用哪只脚访问了这个坐标(x,y)
然后加入优先队列优化   按BFS搜索 第一个到达T的即是答案

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
    int x,y,step,flag;
    friend bool operator<(const node a,node b)        //优先队列 时间少优先
    {
        return a.step>b.step;
    }
} head;
char map[105][105];
int vis[2][105][105];  //三维
int fx[2][9][2]= {{0,1, 0,2, 0,3, 1,1, 1,2, 2,1, -1,1, -1,2, -2,1},{0,-1, 0,-2, 0,-3, 1,-1, 1,-2, 2,-1, -1,-1, -1,-2, -2,-1}}; //左脚和右脚的9个方向
int m,n,ans;
void bfs()
{
    priority_queue<node>q;
    int i,j,tx,ty;
    for(i=1; i<=m; i++)
        for(j=1; j<=n; j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='S')
            {
                q.push((node){i,j,0,0});
                q.push((node){i,j,0,1});                     //左脚右脚开始 都要加入
                vis[0][i][j]=vis[1][i][j]=1;
            }
        }
    while(!q.empty())
    {
        head=q.top();
        q.pop();
        for(i=0; i<9; i++)
        {
            tx=head.x+fx[head.flag][i][0];
            ty=head.y+fx[head.flag][i][1];
            if(tx<1||ty<1||tx>m||ty>n||vis[1-head.flag][tx][ty]||map[tx][ty]=='X')   //当前是head.flag表示的脚     则下一步是1-head.flag表示的脚
                continue;
            vis[1-head.flag][tx][ty]=1;
            if(map[tx][ty]=='T')
            {
                ans=head.step;
                return;
            }
            q.push((node){tx,ty,head.step+map[tx][ty]-'0',1-head.flag});
        }
    }
    return;
}
int main()
{
    while(scanf("%d%d",&n,&m)&&m)
    {
        ans=-1;
        memset(vis,0,sizeof(vis));
        bfs();
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-24 08:31:37

zoj 3103 Cliff Climbing 优先队列+BFS的相关文章

zoj 3103 Cliff Climbing(spfa )

Cliff Climbing Time Limit: 10 Seconds      Memory Limit: 32768 KB At 17:00, special agent Jack starts to escape from the enemy camp. There is a cliff in between the camp and the nearest safety zone. Jack has to climb the almost vertical cliff by step

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

zoj 3811 Untrusted Patrol(bfs或dfs)

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

hdu1242 优先队列+bfs

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22034    Accepted Submission(s): 7843 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

hdu 5040 优先队列BFS+剪枝

(北京网络赛09题)题意:给一矩阵(图),里面有起点,终点,还有探照灯(每个有初始朝向,每秒顺时针转90度),前面有灯或者自己被灯照着,移动就要花3秒,求起点到终点最短时间. 用一个数组三维数组记录一下,用来当前位置当前时间%4有没有灯,然后优先队列(时间短的在前面),搜索即可.考虑到可以来回走或者原地等,不能简单判重剪枝:每个地方最多是4种状态!就是4秒之后就全图状态回到一样!所以若当前状态(时间%4)下来过就不用来了. #include<iostream> #include<vect