A9407 | Maze 2D
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a $2×n$ maze.
Imagine a maze that looks like a $2×n$ rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.
Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a $2×n$ maze.
Imagine a maze that looks like a $2×n$ rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.
Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a $2×n$ maze.
输入格式
The first line contains two integers, $n$ and $m$ $(1<=n<=2·10^{5}; 1<=m<=2·10^{5})$ — the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains $n$ characters, each character equals either '.' (empty cell), or 'X' (obstacle).
Each of the next $m$ lines contains two integers $v_{i}$ and $u_{i}$ $(1<=v_{i},u_{i}<=2n)$ — the description of the $i$ -th request. Numbers $v_{i}$ , $u_{i}$ mean that you need to print the value of the shortest path from the cell of the maze number $v_{i}$ to the cell number $u_{i}$ . We assume that the cells of the first line of the maze are numbered from $1$ to $n$ , from left to right, and the cells of the second line are numbered from $n+1$ to $2n$ from left to right. It is guaranteed that both given cells are empty.
Each of the next $m$ lines contains two integers $v_{i}$ and $u_{i}$ $(1<=v_{i},u_{i}<=2n)$ — the description of the $i$ -th request. Numbers $v_{i}$ , $u_{i}$ mean that you need to print the value of the shortest path from the cell of the maze number $v_{i}$ to the cell number $u_{i}$ . We assume that the cells of the first line of the maze are numbered from $1$ to $n$ , from left to right, and the cells of the second line are numbered from $n+1$ to $2n$ from left to right. It is guaranteed that both given cells are empty.
输出格式
Print $m$ lines. In the $i$ -th line print the answer to the $i$ -th request — either the size of the shortest path or -1, if we can't reach the second cell from the first one.
输入输出样例
输入 #1
4 7 .X.. ...X 5 1 1 3 7 7 1 4 6 1 4 7 5 7
输出 #1
1 4 0 5 2 2 2
输入 #2
10 3 X...X..X.. ..X...X..X 11 7 7 18 18 10
输出 #2
9 -1 3
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted