CodeForces 242C King's Path(bfs+stl)

本文介绍了一种解决超大规模棋盘中寻找从起点到终点最短路径的方法。该问题涉及一个巨大的棋盘(10^9 x 10^9),其中某些单元格为允许通行的区域,而玩家扮演的国王只能在这些允许通行的区域内移动。文章通过使用广度优先搜索(BFS)算法,并结合map和pair数据结构来高效地解决问题。

The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).

You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.

Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.

Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.

Input

The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.

The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.

It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.

Output

If there is no path between the initial and final position along allowed cells, print -1.

Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.

Example
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1

题解:

题意:

给你一个棋盘的起点和终点坐标,你可以往八个方向走,然后下面是你可以走的点的横坐标x,和点纵坐标的范围[l,r],问最少多少步可以走到终点

思路:

比较水的一道bfs,注意点是范围很大,要用map,因为是二维坐标,用到pair,bfs搞的去就好了

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 1008611111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
int dirx[8]={0,0,-1,1,1,1,-1,-1};
int diry[8]={1,-1,0,0,1,-1,-1,1};
struct node
{
    int x,y;
    ll step;
};
int sx,sy,ex,ey;
map<pair<int,int>,int>p;
queue<node>q;
int main()
{
    int i,j,n,m,x,l,r,xx,yy;
    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&x,&l,&r);
        for(j=l;j<=r;j++)
        {
            pair<int,int>now;
            now.first=x;
            now.second=j;
            p[now]=1;
        }
    }
    pair<int,int>temp;
    temp.first=sx;
    temp.second=sy;
    p[temp]=0;
    node now,next;
    now.x=sx;
    now.y=sy;
    now.step=0;
    q.push(now);
    ll res=-1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(i=0;i<8;i++)
        {
            temp.first=now.x+dirx[i];
            temp.second=now.y+diry[i];
            if(p[temp])
            {
                next.x=temp.first;
                next.y=temp.second;
                next.step=now.step+1;
                if(next.x==ex&&next.y==ey)
                {
                    res=next.step;
                    goto loop;
                }
                q.push(next);
                p[temp]=0;
            }
        }
    }
    loop:;
    printf("%d\n",res);
    return 0;
}



当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值