A14940 | Expand the Path
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of testcases.
The first line of each testcase contains the single integer $n$ ( $2 \le n \le 10^8$ ) — the number of rows and columns in the grid.
The second line of each testcase contains a non-empty string $s$ , consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid.
The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$ .
The first line of each testcase contains the single integer $n$ ( $2 \le n \le 10^8$ ) — the number of rows and columns in the grid.
The second line of each testcase contains a non-empty string $s$ , consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid.
The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$ .
输入格式
For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid.
输出格式
In the first testcase, it's enough to consider the following modified paths:
- RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 4)$ , $(3, 4)$ and $(4, 4)$ ;
- RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(2, 3)$ , $(3, 3)$ and $(4, 3)$ ;
- RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(2, 2)$ , $(3, 2)$ and $(4, 2)$ .
Thus, the cells that are visited on at least one modified path are: $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 2)$ , $(2, 3)$ , $(2, 4)$ , $(3, 2)$ , $(3, 3)$ , $(3, 4)$ , $(4, 2)$ , $(4, 3)$ and $(4, 4)$ .
In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.
In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$ , $(2, 1)$ and $(3, 1)$ .
Here are the cells for all testcases:

- RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 4)$ , $(3, 4)$ and $(4, 4)$ ;
- RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(2, 3)$ , $(3, 3)$ and $(4, 3)$ ;
- RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(2, 2)$ , $(3, 2)$ and $(4, 2)$ .
Thus, the cells that are visited on at least one modified path are: $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 2)$ , $(2, 3)$ , $(2, 4)$ , $(3, 2)$ , $(3, 3)$ , $(3, 4)$ , $(4, 2)$ , $(4, 3)$ and $(4, 4)$ .
In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.
In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$ , $(2, 1)$ and $(3, 1)$ .
Here are the cells for all testcases:

输入输出样例
输入 #1
3 4 RD 5 DRDRDRDR 3 D
输出 #1
13 9 3
In the first testcase, it's enough to consider the following modified paths:
- RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 4)$ , $(3, 4)$ and $(4, 4)$ ;
- RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(2, 3)$ , $(3, 3)$ and $(4, 3)$ ;
- RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(2, 2)$ , $(3, 2)$ and $(4, 2)$ .
Thus, the cells that are visited on at least one modified path are: $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 2)$ , $(2, 3)$ , $(2, 4)$ , $(3, 2)$ , $(3, 3)$ , $(3, 4)$ , $(4, 2)$ , $(4, 3)$ and $(4, 4)$ .
In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.
In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$ , $(2, 1)$ and $(3, 1)$ .
Here are the cells for all testcases:

- RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 4)$ , $(3, 4)$ and $(4, 4)$ ;
- RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(2, 3)$ , $(3, 3)$ and $(4, 3)$ ;
- RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$ , $(1, 2)$ , $(2, 2)$ , $(3, 2)$ and $(4, 2)$ .
Thus, the cells that are visited on at least one modified path are: $(1, 1)$ , $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(2, 2)$ , $(2, 3)$ , $(2, 4)$ , $(3, 2)$ , $(3, 3)$ , $(3, 4)$ , $(4, 2)$ , $(4, 3)$ and $(4, 4)$ .
In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.
In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$ , $(2, 1)$ and $(3, 1)$ .
Here are the cells for all testcases:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted