跳棋

题目描述

跳棋是我们很多人小时候喜爱的游戏,今天我们尝试改进一下这个游戏使得它更加有趣。我们设计一个一维的,由很多格子组成的游戏空间,每个格子按照顺序编号为w(1 ≤ w≤ 1,000,000)。我们在这个游戏空间中可以使用跳跃的方式进行运动,但是每次跳跃的格子数必须不大于s(1 ≤ s ≤ 6),我们的最终目标是用最短的时间从起点0到达终点T(每次跳跃耗时为1)。显然,描述至此,这个问题依然很简单,所以我们现在往其中加入一些传送阵,传送阵具有方向性,也就是说传送阵可以从u传送到v,却并不意味着v能够传送到u,我们定义每次传送的时间为0。当然,作为一个有大局观的人,我们可以在跳到一个传送点后选择不传送。

输入

有多组测试数据。
输入的第1行是三个整数T,s和p,其中p代表传送阵的数量(1 ≤ p ≤ 40)。
输入的第2到p+1行包括两个整数a和b代表有一个传送阵可以使得我们从a点传送到b点。

输出

对应每组测试数据输出一个整数,代表从起点到达终点最少需要的时间。

样例输入

28 3 5
2 18 
5 13 
12 6
17 25 
20 15
50 6 1
9 45

样例输出

3
3

这道题可以看成最短路劲来做也可以看成搜索来做,因为比较擅长搜索所以用搜索来做,很简单一个水题!!!

 

 

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cstring>
# include <queue>

using namespace std;

# define ll long long int
# define For(i, j, k) for(int i = j, i < k; i++)

const int maxx = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
int t, s, p;
int book[maxn], pos[maxn];//pos对应x位置的传送地点
struct node
{
    int x, y;
};
void bfs()
{
    queue<struct node> q;
    struct node temp, front;
    temp.x = 0;
    temp.y = 0;
    q.push(temp);
    book[0] = 1;
    while(!q.empty())
    {
        front = q.front();
        q.pop();
        if(front.x == t)
        {
            cout << front.y << endl;
            return ;
        }
        for(int i = front.x - s; i <= front.x + s; i++)
        {
            if(i >= 0 && !book[i])
            {
                book[i] = true;
                temp.x = i;
                temp.y = front.y + 1;
                q.push(temp);
                if(pos[i] != -1 && !book[pos[i]])
                {
                    temp.x = pos[i];
                    book[temp.x] = 1;
                    q.push(temp);
                }
            }
        }
    }
}
int main(int argc,char *argv[])
{
    while(cin >> t >> s >> p)
    {
        memset(book, false, sizeof(book));
        memset(pos, -1, sizeof(pos));
        for(int i = 0; i < p; i++)
        {
            int x, y;
            cin >> x >> y;
            pos[x] = y;
        }
        bfs();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值