CodeForces - 242C King's Path

6 篇文章 0 订阅

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值