Shortest Path with Obstacle( CodeForces - 1547A )(模拟)

There are three cells on an infinite 2-dimensional grid, labeled  A,  B, and  F. Find the length of the shortest path from  A to BB if:

  • in one move you can go to any of the four adjacent cells sharing a side;
  • visiting the cell  F is forbidden (it is an obstacle).

Input

The first line contains an integer t (1≤t≤10^4 ) — the number of test cases in the input. Then  t test cases follow. Before each test case, there is an empty line.

Each test case contains three lines. The first one contains two integers xA,yA  ( 1≤xA,yA≤1000) — coordinates of the start cell A . The second one contains two integers  xB,yB ( 1≤xB,yB≤1000) — coordinates of the finish cell  B. The third one contains two integers xF,yF ( 1≤xF,yF≤1000) — coordinates of the forbidden cell F . All cells are distinct.

Coordinate x corresponds to the column number and coordinate y  corresponds to the row number (see the pictures below).

Output

Output t  lines. The i -th line should contain the answer for the  i-th test case: the length of the shortest path from the cell A  to the cell B  if the cell F is not allowed to be visited.

Example

Input

7

1 1
3 3
2 2

2 5
2 1
2 3

1000 42
1000 1
1000 1000

1 10
3 10
2 10

3 8
7 8
3 7

2 1
4 1
1 1

1 344
1 10
1 1

Output

4
6
41
4
4
2
334

Note

 An example of a possible shortest path for the first test case.

 

 An example of a possible shortest path for the second test case.

题目分析

平面直角坐标系上有两个点和一个障碍。从一个点出发到另一个点,可以走上下左右,不能走障碍,求最短路的长度。

做法

我们发现大部分情况下障碍是不会造成影响的。只有当三个点横坐标相同或者纵坐标相同时且障碍在亮点中间时才会有效。具体实现看代码。

 

#include<bits/stdc++.h>
using namespace std;
int e[1005][1005];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
	//	memset(e,0,sizeof(e));
		int x1,y1,x2,y2,a,b;
		cin>>x1>>y1>>x2>>y2>>a>>b;
		if(x1!= x2 && y1 != y2)
		{
			cout<<abs(x1 - x2) + abs(y1 - y2)<<endl;
		}
		else
		{
			if(x1 == x2 && y1 == y2) //|| (x1 == a && y1 == b) || (x2 == a && y2 == b))) 
			{
				cout<<"0"<<endl;
			break;
			}
			if(x1 == x2)
			{
				if(max(y1,y2) > b && min(y1,y2) < b && x1 == a)
				{
					cout<<abs(y1 - y2) + 2<<endl;
				}
				else
				{
					cout<<abs(y1 - y2)<<endl;
				}
			}
		  if(y1==y2)
			{
				if(max(x1,x2) > a && min(x1,x2) < a && y1 == b)
				{
					cout<<abs(x1 - x2) + 2<<endl;
				}
				else
				{
					cout<<abs(x1 - x2)<<endl;
				}
			}
		}
	}
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值