CF821D.Okabe and City
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren.
Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to n from top to bottom, and columns are numbered 1 to m from left to right. Exactly k cells in the city are lit by a street lamp. It's guaranteed that the top-left cell is lit.
Okabe starts his walk from the top-left cell, and wants to reach the bottom-right cell. Of course, Okabe will only walk on lit cells, and he can only move to adjacent cells in the up, down, left, and right directions. However, Okabe can also temporarily light all the cells in any single row or column at a time if he pays 1 coin, allowing him to walk through some cells not lit initially.
Note that Okabe can only light a single row or column at a time, and has to pay a coin every time he lights a new row or column. To change the row or column that is temporarily lit, he must stand at a cell that is lit initially. Also, once he removes his temporary light from a row or column, all cells in that row/column not initially lit are now not lit.
Help Okabe find the minimum number of coins he needs to pay to complete his walk!
Okabe 喜欢沿着路灯照亮的路径穿行于他的城市中,这样他就不会被小学生殴打了。
Okabe 的城市由一个 2 维网格表示,其中行从上到下编号为 1 到 n,列从左到右编号为 1 到 m。城市中恰好有 k 个格子被路灯照亮。保证左上角格子(即 (1,1))是被照亮的。
Okabe 从左上角格子出发,希望到达右下角格子。当然,Okabe 只能在被照亮的格子上行走,且每次只能向上、下、左、右四个方向移动至相邻格子。然而,Okabe 还可以每次花费 1 枚硬币,临时点亮某一行或某一列的所有格子,从而允许他经过一些初始未被照亮的格子。
注意:Okabe 每次仅能临时点亮一行或一列,且每次点亮新的行或列均需支付 1 枚硬币。若要切换当前被临时点亮的行或列,他必须站在一个初始就被照亮的格子上。此外,一旦他取消对某一行或列的临时照明,则该行/列中所有初始未被照亮的格子将立即变为未照亮状态。
请帮助 Okabe 计算完成此次行走所需的最少硬币数!
输入格式
The first line of input contains three space-separated integers n, m, and k (2 ≤ n, m, k ≤ 104).
Each of the next k lines contains two space-separated integers r__i and c__i (1 ≤ r__i ≤ n, 1 ≤ c__i ≤ m) — the row and the column of the i-th lit cell.
It is guaranteed that all k lit cells are distinct. It is guaranteed that the top-left cell is lit.
输入的第一行包含三个用空格分隔的整数 n、m 和 k(2 ≤ n, m, k ≤ 104)。
接下来的 k 行,每行包含两个用空格分隔的整数 ri 和 ci(1 ≤ ri ≤ n, 1 ≤ ci ≤ m),表示第 i 个被点亮的单元格所在的行与列。
保证这 k 个被点亮的单元格互不相同,且保证左上角的单元格(即 (1,1))被点亮。
输出格式
Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it's not possible.
输出 Okabe 完成步行所需支付的最少硬币数量;如果无法完成步行,则输出 −1。
输入输出样例
输入#1
4 4 5 1 1 2 1 2 3 3 3 4 3
输出#1
2
输入#2
5 5 4 1 1 2 1 3 1 3 2
输出#2
-1
输入#3
2 2 4 1 1 1 2 2 1 2 2
输出#3
0
输入#4
5 5 4 1 1 2 2 3 3 4 4
输出#4
3
说明/提示
In the first sample test, Okabe can take the path
, paying only when moving to (2, 3) and (4, 4).
In the fourth sample, Okabe can take the path
, paying when moving to (1, 2), (3, 4), and (5, 4).
在第一个样例测试中,Okabe 可以选择路径
,仅在移动至点 (2,3) 和 (4,4) 时付费。
在第四个样例中,Okabe 可以选择路径
,在移动至点 (1,2)、(3,4) 和 (5,4) 时付费。
输入解题思路,AI测评打分。不知道怎么写?