CF1797A.Li Hua and Maze

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a rectangular maze of size n×mn\times m. Denote (r,c)(r,c) as the cell on the rr-th row from the top and the cc-th column from the left. Two cells are adjacent if they share an edge. A path is a sequence of adjacent empty cells.

Each cell is initially empty. Li Hua can choose some cells (except (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2)) and place an obstacle in each of them. He wants to know the minimum number of obstacles needed to be placed so that there isn't a path from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2).

Suppose you were Li Hua, please solve this problem.

有一个大小为 n×mn\times m 的矩形迷宫。记 (r,c)(r,c) 表示从上往下数第 rr 行、从左往右数第 cc 列的格子。若两个格子共享一条边,则称它们相邻。一条路径是指一系列相邻的空格子组成的序列。

每个格子初始均为空。李华可以选择若干个格子(但不能选 (x1,y1)(x_1, y_1) 和 (x2,y2)(x_2, y_2))并在其中各放置一个障碍物。他想知道:为使得从 (x1,y1)(x_1, y_1) 到 (x2,y2)(x_2, y_2) 不存在任何路径,所需放置的障碍物的最少数量是多少?

假设你是李华,请解决此问题。

输入格式

The first line contains the single integer tt (1≤t≤5001 \le t \le 500) — the number of test cases.

The first line of each test case contains two integers n,mn,m (4≤n,m≤1094\le n,m\le 10^9) — the size of the maze.

The second line of each test case contains four integers x1,y1,x2,y2x_1,y_1,x_2,y_2 (1≤x1,x2≤n,1≤y1,y2≤m1\le x_1,x_2\le n, 1\le y_1,y_2\le m) — the coordinates of the start and the end.

It is guaranteed that ∣x1−x2∣+∣y1−y2∣≥2|x_1-x_2|+|y_1-y_2|\ge 2.

第一行包含一个整数 tt(1≤t≤5001 \le t \le 500)——测试用例的数量。

每个测试用例的第一行包含两个整数 n,mn,m(4≤n,m≤1094\le n,m\le 10^9)——迷宫的尺寸。

每个测试用例的第二行包含四个整数 x1,y1,x2,y2x_1,y_1,x_2,y_2(1≤x1,x2≤n, 1≤y1,y2≤m1\le x_1,x_2\le n,\ 1\le y_1,y_2\le m)——起点和终点的坐标。

保证 ∣x1−x2∣+∣y1−y2∣≥2|x_1-x_2|+|y_1-y_2|\ge 2。

输出格式

For each test case print the minimum number of obstacles you need to put on the field so that there is no path from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2).

对于每个测试用例,输出需要在场地上放置的障碍物的最少数量,使得从 (x1,y1)(x_1, y_1) 到 (x2,y2)(x_2, y_2) 不存在路径。

输入输出样例

  • 输入#1

    3
    4 4
    2 2 3 3
    6 7
    1 1 2 3
    9 9
    5 1 3 6

    输出#1

    4
    2
    3

说明/提示

In test case 1, you can put obstacles on (1,3),(2,3),(3,2),(4,2)(1,3), (2,3), (3,2), (4,2). Then the path from (2,2)(2,2) to (3,3)(3,3) will not exist.

在测试用例 1 中,你可以在格子 (1,3)(1,3)、(2,3)(2,3)、(3,2)(3,2) 和 (4,2)(4,2) 上放置障碍物。此时,从 (2,2)(2,2) 到 (3,3)(3,3) 的路径将不存在。

输入解题思路,AI测评打分。不知道怎么写?

首页