AT_ndpc2026_s.Two doors

入门

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an N×NN \times N grid. Let (i,j)(i,j) denote the cell in the ii-th row from the top and the jj-th column from the left.

The state between adjacent cells (sharing an edge) is represented by a character cc:

  • If c=c = ., there is nothing between the cells, and you can pass freely.
  • If c=c = D, there is a door, and you can pass freely.
  • If c=c = A, there is a special door called AA, and you can pass freely.
  • If c=c = B, there is a special door called BB, and you can pass freely.
  • If c=c = #, there is a wall, and you cannot pass.

Here, there is exactly one door AA and one door BB in the grid.

The states between adjacent cells are given by strings S1,S2,…,SN−1S_1, S_2, \dots, S_{N-1} and T1,T2,…,TNT_1, T_2, \dots, T_N:

  • For 1≤i≤N−11 \leq i \leq N-1, 1≤j≤N1 \leq j \leq N, the state between (i,j)(i,j) and (i+1,j)(i+1,j) is Si,jS_{i,j}.
  • For 1≤i≤N1 \leq i \leq N, 1≤j≤N−11 \leq j \leq N-1, the state between (i,j)(i,j) and (i,j+1)(i,j+1) is Ti,jT_{i,j}.

A grid is called a good state if you can start from (1,1)(1,1) and reach (N,N)(N,N) by moving up, down, left, or right without passing through walls.

You will block some of the doors (except AA and BB), 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 AA or BB, the grid is still in a good state.
  • If you additionally block both AA and BB, 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 TT test cases. Solve each of them.

给你一个 N×NN \times N 的网格。记 (i,j)(i,j) 表示从上往下数第 ii 行、从左往右数第 jj 列的格子。

相邻格子(即共享一条边的格子)之间的状态由一个字符 cc 表示:

  • 若 c=c = .,则两格之间无障碍,可自由通行;
  • 若 c=c = D,则两格之间有一扇普通门,可自由通行;
  • 若 c=c = A,则两格之间有一扇特殊门 AA,可自由通行;
  • 若 c=c = B,则两格之间有一扇特殊门 BB,可自由通行;
  • 若 c=c = #,则两格之间有一堵墙,不可通行。

其中,整个网格中恰好存在一扇门 AA 和一扇门 BB。

相邻格子之间的状态由字符串 S1,S2,…,SN−1S_1, S_2, \dots, S_{N-1} 和 T1,T2,…,TNT_1, T_2, \dots, T_N 给出:

  • 对于 1≤i≤N−11 \leq i \leq N-1,1≤j≤N1 \leq j \leq N,格子 (i,j)(i,j) 与 (i+1,j)(i+1,j) 之间的状态为 Si,jS_{i,j};
  • 对于 1≤i≤N1 \leq i \leq N,1≤j≤N−11 \leq j \leq N-1,格子 (i,j)(i,j) 与 (i,j+1)(i,j+1) 之间的状态为 Ti,jT_{i,j}。

若能从 (1,1)(1,1) 出发,仅通过上下左右移动(不穿过墙),到达 (N,N)(N,N),则称该网格处于良好状态(good state)。

你需要将部分门(但不能封锁 AA 和 BB) 封锁(使其不可通行),使得满足以下全部条件:

  • 网格处于良好状态;
  • 在此基础上,额外封锁 AA 和 BB 中的恰好一个后,网格仍处于良好状态;
  • 但在基础上,额外封锁 AA 和 BB 这两个门后,网格不再处于良好状态。

是否可能满足上述所有条件?若可能,求出所需封锁的门的最少数量。

你将收到 TT 组测试用例,请对每组分别求解。

输入格式

The input is given from standard input in the following format:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

Each test case is given in the following format:

NN
S1S_1
S2S_2
⋮\vdots
SN−1S_{N-1}
T1T_1
T2T_2
⋮\vdots
TNT_N

输入从标准输入中按以下格式给出:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

每个测试用例按以下格式给出:

NN
S1S_1
S2S_2
⋮\vdots
SN−1S_{N-1}
T1T_1
T2T_2
⋮\vdots
TNT_N

输出格式

Print TT lines. On the ii-th line, output the answer for the ii-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.

输出 TT 行。第 ii 行输出第 ii 个测试用例的答案。
对于每个测试用例,如果能够满足条件,则输出需要封锁的门的最少数量;否则,输出 -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≤1051 \leq T \leq 10^5
  • 2≤N≤402 \leq N \leq 40
  • Each SiS_i is a string of length NN consisting of ., D, A, B, #
  • Each TiT_i is a string of length N−1N-1 consisting of ., D, A, B, #
  • A and B each appear exactly once among all Si,jS_{i,j} and Ti,jT_{i,j}
  • The sum of N2N^2 over all test cases is at most 2×1052 \times 10^5
  • The sum of N4N^4 over all test cases is at most 40440^4

样例 1 解释:
例如,在第一个测试用例中,条件已经满足。

约束条件

  • 1≤T≤1051 \leq T \leq 10^5
  • 2≤N≤402 \leq N \leq 40
  • 每个 SiS_i 是一个长度为 NN 的字符串,由字符 ., D, A, B, # 组成
  • 每个 TiT_i 是一个长度为 N−1N-1 的字符串,由字符 ., D, A, B, # 组成
  • 在所有 Si,jS_{i,j} 和 Ti,jT_{i,j} 中,字符 A 和 B 各恰好出现一次
  • 所有测试用例中 N2N^2 的总和不超过 2×1052 \times 10^5
  • 所有测试用例中 N4N^4 的总和不超过 40440^4

输入解题思路,AI测评打分。不知道怎么写?

首页