CodeForces - 242C King's Path

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
 
     
题意:给你一个图的终点和起点,问你能否从起点走到终点,有八个方向可以走,题目并没有给完整的图,只给了图的某一行的一段区域可以走,具体请看下面;
第一行:四个数,给你两个点,一个是起点一个是终点;
第二行:一个数n,代表接下来有n组输入
接下来n行:三个数,ri,ai,bi,表示,第ri行的ai-bi之间可以走;
比如第一组样例代表;起点是(5,7),终点(6,11),
接下来有三组样例:
5 3 8 表示图的第5行的第3到第8可以走,即5,3;5,4;5,5;5,6;5,7;5,8 六个点可以走。
 
     
思路:因为图很大,开数组肯定是不行的,所以只能用STL容器;
用pair表示点,一pair为键存在map里面,然后map表示路径;在简单bfs一下就可以了
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define eps 1e-10
#define maxn 300005
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson step<<1
#define rson step<<1|1
#define MOD 1000000009
#define sqr(a) ((a)*(a))
using namespace std;
int d[8][2] = {0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1};
int sx, sy, ex, ey;         //起点和终点
pair<int,int> p1,p2;        //用pair记录点 
map<pair<int,int>,int> mat; //以pair为键,记录步数; 
queue<pair<int,int> > q;    //把pair压入队列 
void bfs() {
	q.push(p1);
	mat[p1] = 1;
	while(!q.empty()) {
		p1 = q.front();     //转变 
		q.pop();
		for(int i = 0; i < 8; i++) {
			p2.first = p1.first + d[i][0];
			p2.second = p1.second + d[i][1];
			if(mat[p2] == -1) {
				mat[p2] = mat[p1] + 1;
				q.push(p2);
			}
		}
	}
	return ;
}
int main() {
	int n;
	int r, a, b;
	scanf("%d%d%d%d", &sx, &sy, &ex, &ey); 
	scanf("%d",&n);
	while(n--) {
		scanf("%d%d%d", &r, &a, &b);
		for(int i = a; i <= b; i++) {
			mat[make_pair(r,i)] = -1;
		}
	}
	p1.first = sx;
	p1.second = sy;
	bfs();
	int m = mat[make_pair(ex,ey)];
	if(m > 0) m--;
	printf("%d\n",m); 
	return 0;
}


编号 文件名称 cwts-specs-001 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:名语术语 cwts-specs-002 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:概述 cwts-specs-003 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:物理信道和传输信道到物理信道的映射 cwts-specs-004 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:信道编码与复用 cwts-specs-005 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:扩频与调制 cwts-specs-006 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:物理层过程 cwts-specs-007 IMT-DS FDD(WCDMA)系统无线接口物理层技术规范:物理层测量 cwts-specs-008 IMT-DS FDD(WCDMA)系统无线接口层2技术规范:物理层向上层提供的服务 cwts-specs-009 IMT-DS FDD(WCDMA)系统无线接口层2技术规范:MAC协议 cwts-specs-010 IMT-DS FDD(WCDMA)系统无线接口层2技术规范:RLC协议 cwts-specs-011 IMT-DS FDD(WCDMA)系统无线接口层2技术规范:PDCP协议 cwts-specs-012 IMT-DS FDD(WCDMA)系统无线接口层2技术规范:BMC协议 cwts-specs-013 IMT-DS FDD(WCDMA)系统无线接口层3技术规范:RRC协议 cwts-specs-014 IMT-DS FDD(WCDMA)系统Iu接口技术规范:概述 cwts-specs-015 IMT-DS FDD(WCDMA)系统Iu接口技术规范:层1技术要求 cwts-specs-016 IMT-DS FDD(WCDMA)系统Iu接口技术规范:信令传输 cwts-specs-017 IMT-DS FDD(WCDMA)系统Iu接口技术规范:RANAP信令 cwts-specs-018 IMT-DS FDD(WCDMA)系统Iu接口技术规范:数据传输和传输信令 cwts-specs-019 IMT-DS FDD(WCDMA)系统Iu接口技术规范:用户平面协议 cwts-specs-020 IMT-DS FDD(WCDMA)系统Iub接口技术规范:概述 cwts-specs-021 IMT-DS FDD(WCDMA)系统Iub接口技术规范:层1技术要求 cwts-specs-022 IMT-DS FDD(WCDMA)系统Iub接口技术规范:信令传输 cwts-specs-023 IMT-DS FDD(WCDMA)系统Iub接口技术规范:NBAP信令 cwts-specs-024 IMT-DS FDD(WCDMA)系统Iub接口技术规范:用于CCH数据流的数据传输和传输信令 cwts-specs-025 IMT-DS FDD(WCDMA)系统Iub接口技术规范:用于CCH数据流的用户平面协议 cwts-specs-026 IMT-DS FDD(WCDMA)系统Iur接口技术规范:概述 cwts-specs-027 IMT-DS FDD(WCDMA)系统Iur接口技术规范:层1技术要求 cwts-specs-028 IMT-DS FDD(WCDMA)系统Iur接口技术规范:信令传输 cwts-specs-029 IMT-DS FDD(WCDMA)系统Iur接口技术规范:RNSAP信令 cwts-specs-030 IMT-DS FDD(WCDMA)系统Iur接口技术规范:用于CCH数据流的数据传输和传输信令 cwts-specs-031 IMT-DS FDD(WCDMA)系统Iur接口技术规范:用于CCH数据流的用户平面协议 cwts-specs-032 IMT-DS FDD(WCDMA)系统Iub/Iur接口技术规范:用于DCH数据流的数据传输和传输信令 cwts-specs-033 IMT-DS FDD(WCDMA)系统Iub/Iur接口技术规范:用于DCH数据流的用户平面协议 cwts-specs-034 TD-SCDMA系统无线接口物理层技术规范 cwts-specs-035 TD-SCDMA系统无线接口层2技术规范 cwts-specs-036 TD-SCDMA系统无线接口层3-RRC技术规范 cwts-specs-037 TD-SCDMA系统Iu接口技术规范 cwts-specs-038 TD-SCDMA系统Iub接口技术规范 cwts-specs-039 TD-SCDMA系统Iur接口技术规范 cwts-specs-040 TD-SCDMA系统基站设备无线收发特性技术规范 cwts-specs-041 TD-SCDMA系统用户终端设备无线收发特性技术规范 CWTS发布的研究报告列表 cwts-reports-001 IMT-DS FDD(WCDMA)系统连接模式下的层间过程(25.303)标准研究报告 cwts-reports-002 IMT-DS FDD(WCDMA)系统空闲模式下UE的流程和连接模式下小区重选流程(25.304)标准研究报告 cwts-reports-003 IMT-DS FDD(WCDMA)系统无线资源管理RRM研究报告 cwts-reports-004 IMT-DS FDD(WCDMA)系统无线资源管理RRM研究报告 cwts-reports-005 IMT-DS FDD(WCDMA)系统UE无线接入能力研究报告
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值