A9321 | Volcanoes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Iahub got lost in a very big desert. The desert can be represented as a $n×n$ square matrix, where each cell is a zone of the desert. The cell $(i,j)$ represents the cell at row $i$ and column $j$ $(1<=i,j<=n)$ . Iahub can go from one cell $(i,j)$ only down or right, that is to cells $(i+1,j)$ or $(i,j+1)$ .
Also, there are $m$ cells that are occupied by volcanoes, which Iahub cannot enter.
Iahub is initially at cell $(1,1)$ and he needs to travel to cell $(n,n)$ . Knowing that Iahub needs $1$ second to travel from one cell to another, find the minimum time in which he can arrive in cell $(n,n)$ .
Also, there are $m$ cells that are occupied by volcanoes, which Iahub cannot enter.
Iahub is initially at cell $(1,1)$ and he needs to travel to cell $(n,n)$ . Knowing that Iahub needs $1$ second to travel from one cell to another, find the minimum time in which he can arrive in cell $(n,n)$ .
输入格式
The first line contains two integers $n$ $(1<=n<=10^{9})$ and $m$ $(1<=m<=10^{5})$ . Each of the next $m$ lines contains a pair of integers, $x$ and $y$ $(1<=x,y<=n)$ , representing the coordinates of the volcanoes.
Consider matrix rows are numbered from 1 to $n$ from top to bottom, and matrix columns are numbered from 1 to $n$ from left to right. There is no volcano in cell $(1,1)$ . No two volcanoes occupy the same location.
Consider matrix rows are numbered from 1 to $n$ from top to bottom, and matrix columns are numbered from 1 to $n$ from left to right. There is no volcano in cell $(1,1)$ . No two volcanoes occupy the same location.
输出格式
Print one integer, the minimum time in which Iahub can arrive at cell $(n,n)$ . If no solution exists (there is no path to the final cell), print -1.
输入输出样例
输入 #1
4 2 1 3 1 4
输出 #1
6
输入 #2
7 8 1 6 2 6 3 5 3 6 4 3 5 1 5 2 5 3
输出 #2
12
输入 #3
2 2 1 2 2 1
输出 #3
-1
Consider the first sample. A possible road is: $(1,1)$ $→$ $(1,2)$ $→$ $(2,2)$ $→$ $(2,3)$ $→$ $(3,3)$ $→$ $(3,4)$ $→$ $(4,4)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted