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

简介: 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


ac287c426dcdadb26a8ca88623ca68f5.png


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


相关文章
|
存储 测试技术
单链表的模拟实现
单链表的模拟实现
107 0
|
编译器 C++ 容器
C++按值返回对象那些事
C++按值返回对象那些事
332 0
|
前端开发 HTML5 移动开发
200佳优秀的精美网页欣赏网站推荐(系列八)
  这个系列将向大家推荐创意网页设计欣赏的200佳网站。网页设计师们可通过这些网站收集的优秀网页设计作品来获取灵感,进而设计出更加时尚、更有创意的作品,网页设计师也可以把自己得意的作品提交到这些网站,让更多的设计师朋友欣赏到你的网页作品。
983 0
|
3天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
13天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
509 203
|
5天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
730 157

热门文章

最新文章