AT_ndpc2026_s.Two doors
入门
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an N×N grid. Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The state between adjacent cells (sharing an edge) is represented by a character c:
- If c=
., there is nothing between the cells, and you can pass freely. - If c=
D, there is a door, and you can pass freely. - If c=
A, there is a special door called A, and you can pass freely. - If c=
B, there is a special door called B, and you can pass freely. - If c=
#, there is a wall, and you cannot pass.
Here, there is exactly one door A and one door B in the grid.
The states between adjacent cells are given by strings S1,S2,…,SN−1 and T1,T2,…,TN:
- For 1≤i≤N−1, 1≤j≤N, the state between (i,j) and (i+1,j) is Si,j.
- For 1≤i≤N, 1≤j≤N−1, the state between (i,j) and (i,j+1) is Ti,j.
A grid is called a good state if you can start from (1,1) and reach (N,N) by moving up, down, left, or right without passing through walls.
You will block some of the doors (except A and B), making them impassable, so that the following conditions are satisfied:
- The grid is in a good state.
- Even if you additionally block exactly one of A or B, the grid is still in a good state.
- If you additionally block both A and B, the grid is no longer in a good state.
Is it possible to satisfy these conditions? If it is possible, find the minimum number of doors you need to block.
You are given T test cases. Solve each of them.
给你一个 N×N 的网格。记 (i,j) 表示从上往下数第 i 行、从左往右数第 j 列的格子。
相邻格子(即共享一条边的格子)之间的状态由一个字符 c 表示:
- 若 c=
.,则两格之间无障碍,可自由通行; - 若 c=
D,则两格之间有一扇普通门,可自由通行; - 若 c=
A,则两格之间有一扇特殊门 A,可自由通行; - 若 c=
B,则两格之间有一扇特殊门 B,可自由通行; - 若 c=
#,则两格之间有一堵墙,不可通行。
其中,整个网格中恰好存在一扇门 A 和一扇门 B。
相邻格子之间的状态由字符串 S1,S2,…,SN−1 和 T1,T2,…,TN 给出:
- 对于 1≤i≤N−1,1≤j≤N,格子 (i,j) 与 (i+1,j) 之间的状态为 Si,j;
- 对于 1≤i≤N,1≤j≤N−1,格子 (i,j) 与 (i,j+1) 之间的状态为 Ti,j。
若能从 (1,1) 出发,仅通过上下左右移动(不穿过墙),到达 (N,N),则称该网格处于良好状态(good state)。
你需要将部分门(但不能封锁 A 和 B) 封锁(使其不可通行),使得满足以下全部条件:
- 网格处于良好状态;
- 在此基础上,额外封锁 A 和 B 中的恰好一个后,网格仍处于良好状态;
- 但在基础上,额外封锁 A 和 B 这两个门后,网格不再处于良好状态。
是否可能满足上述所有条件?若可能,求出所需封锁的门的最少数量。
你将收到 T 组测试用例,请对每组分别求解。
输入格式
The input is given from standard input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N
S1
S2
⋮
SN−1
T1
T2
⋮
TN
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N
S1
S2
⋮
SN−1
T1
T2
⋮
TN
输出格式
Print T lines. On the i-th line, output the answer for the i-th test case.
For each test case, if it is possible to satisfy the conditions, output the minimum number of doors that need to be blocked; otherwise, output -1.
输出 T 行。第 i 行输出第 i 个测试用例的答案。
对于每个测试用例,如果能够满足条件,则输出需要封锁的门的最少数量;否则,输出 -1。
输入输出样例
输入#1
6 2 .A . B 2 .A # B 3 #D. #BD .A .# .. 4 DDBD ..#D #D.D #DD #DD .A. #.# 4 D.#D DD#. D.B. #D. .#D ... .DA 9 DDD.D#DDD DDDADDDDD DD.DDDDDD DDDD#.DDD DD#DDDD.# DDDDD#.#D DD.#..DDD DDDDD#D.D DDD...DD D#.D#D#D D#DD#D.# DDD#DD## BDDD.D#D DD#DDDDD DDDDD#DD DDD#DDDD ##DDD.#D
输出#1
0 -1 0 3 -1 7
说明/提示
Sample 1 Explanation:
For example, in the first test case, the conditions are already satisfied.
Constraints
- 1≤T≤105
- 2≤N≤40
- Each Si is a string of length N consisting of
.,D,A,B,# - Each Ti is a string of length N−1 consisting of
.,D,A,B,# AandBeach appear exactly once among all Si,j and Ti,j- The sum of N2 over all test cases is at most 2×105
- The sum of N4 over all test cases is at most 404
样例 1 解释:
例如,在第一个测试用例中,条件已经满足。
约束条件
- 1≤T≤105
- 2≤N≤40
- 每个 Si 是一个长度为 N 的字符串,由字符
.,D,A,B,#组成 - 每个 Ti 是一个长度为 N−1 的字符串,由字符
.,D,A,B,#组成 - 在所有 Si,j 和 Ti,j 中,字符
A和B各恰好出现一次 - 所有测试用例中 N2 的总和不超过 2×105
- 所有测试用例中 N4 的总和不超过 404
输入解题思路,AI测评打分。不知道怎么写?