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


相关文章
|
9月前
|
Rust 中间件 API
BustAPI:当 Python 遇上 Rust,Web 框架也能“起飞“
BustAPI 是融合 Python 易用性与 Rust 高性能的 Web 框架:基于 PyO3 封装 Actix-Web,保留 Flask 风格语法,请求性能提升 10–50 倍;支持自动文档、类型校验、异步、中间件等生产级功能,迁移零成本,部署极简——让 Python 服务轻松应对高并发。
765 5
|
9月前
|
设计模式 监控 JavaScript
VUE前端进阶知识整理(终)
教程来源 https://app-aaqhxe29haf5.appmiaoda.com 本文系统讲解Vue进阶核心:设计模式(发布-订阅、工厂、组合、策略、装饰器)在源码与开发中的应用;企业级工程化实践(Vite+TS+Pinia+RBAC权限+Vitest测试+监控告警);以及Vue 3.6+新特性(Vapor Mode无VDOM、编译优化)。助力开发者从使用者蜕变为架构设计者。
|
9月前
|
存储 缓存 监控
微服务越拆越乱?8 大致命反模式拆解与架构重构全实战
本文剖析微服务常见八大反模式(如分布式单体、数据库共享、过度拆分等),揭示其违背的核心设计原则,并提供可落地的重构方案、代码实现与治理方法论,助团队走出架构乱象,回归微服务本质价值。
512 1
|
11月前
|
存储 缓存 监控
Python装饰器实战:从入门到封装通用工具
Python装饰器是“函数外套”,不改原代码即可动态增强功能。本文详解其原理(@语法糖、wrapper封装)、进阶技巧(保留元信息、带参装饰器、多层叠加)及企业级应用(性能监控、缓存、权限校验),涵盖类装饰器、工厂模式与最佳实践,助你写出优雅可维护的代码。(239字)
498 1
|
11月前
|
缓存 监控 Java
B2Bitem_search - 根据关键词获取商标列表接口对接全攻略:从入门到精通
B2Bitem_search是面向B2B平台、知产机构及企业风控系统的商标批量检索核心接口,支持关键词、尼斯分类、状态等多维筛选,分页排序、字段过滤,数据直连国家知识产权局,实时权威。含全链路对接指南。
|
缓存 人工智能 监控
通义灵码进阶指南:解锁智能编程的隐藏技能
通义灵码是阿里云推出的智能编程助手,已突破简单代码补全功能,成为全栈开发导航仪、架构思维催化剂、代码质量监督员和知识检索加速器。本文从基础到进阶,详细介绍了其高效操作技巧,包括精准生成、对话式编程、代码重构及技术文档交互等功能。同时提供团队级最佳实践、专家级配置指南及避坑建议,并展望未来实验性功能。通过将其视为“编程伙伴”,开发者可实现更高效的人机协作,优化工作流并提升生产力。
1284 6
|
11月前
|
数据采集
静态IP购买常见问题及解决方案
静态IP因稳定、适用广,成企业刚需,但购买易踩坑。本文梳理IP纯度低、带宽虚标、地域偏差、售后差、计费不透明、合规风险6大问题,提供避坑指南,助你高效选购合规、稳定的独享静态IP,保障业务顺畅运行。(238字)
|
开发框架 Java 编译器
2025年1月推荐-工欲善其事,必先利其器-程序员必备之-核心基本工具—不要看什么国际排行榜-没有用-编辑器和编译器推荐-优雅草央千澈
2025年1月推荐-工欲善其事,必先利其器-程序员必备之-核心基本工具—不要看什么国际排行榜-没有用-编辑器和编译器推荐-优雅草央千澈
1012 1
|
12月前
|
安全
CSRF攻击
CSRF(跨站请求伪造)攻击利用用户已登录状态,诱导其发起非本意的请求。攻击者构造恶意请求,通过钓鱼网站等渠道使用户在不知情下操作目标网站,如发帖、转账等。防御手段包括:使用Token验证、SameSite Cookie属性、检查Referer、禁止第三方携带Cookie等,以保障用户安全。
|
计算机视觉
Opencv学习笔记(五):cv2.putText()和cv2.rectangle()详细理解
这篇文章详细介绍了OpenCV库中的`cv2.putText()`和`cv2.rectangle()`函数的使用方法,并通过一个实战例子展示了如何使用这些函数在图像上绘制文字和矩形框。
1701 0
Opencv学习笔记(五):cv2.putText()和cv2.rectangle()详细理解