CF2245H.Connect Connect See
NOI/NOI+/CTSC
通过率:0%
时间限制:8.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a rectangular grid a consisting of n rows and m columns. The j-th cell in the i-th row is denoted by (i,j). There is a non-negative integer written in every cell. The integer written in cell (i,j) is denoted by ai,j.
A path p of length k on a is defined as a sequence of cells p0,p1,…,pk such that pi and pi+1 share an edge for every 0≤i<k, and all pi are distinct. The number of turns of a simple path p of length k, denoted by t(p), is defined as the number of indices 1≤i<k that satisfy the following condition:
- Let pj=(xj,yj) for j∈i−1,i,i+1. Then, both xi−1=xi+1 and yi−1=yi+1 hold.
A path p=[(x0,y0),(x1,y1),…,(xk,yk)] is considered valid if and only if all of the following conditions hold:
- k≥1.
- t(p)≤2.
- ax0,y0=axk,yk, ax0,y0>0, and axi,yi=0 for every 1≤i<k.
An unordered pair of cells (u1,v1) and (u2,v2) is considered connectable if and only if there exists a valid path p of length k such that p0=(u1,v1) and pk=(u2,v2).
Find the number of connectable pairs in a.
给你一个由 n 行和 m 列组成的矩形网格 a。第 i 行第 j 列的格子记为 (i,j)。每个格子中写有一个非负整数。格子 (i,j) 中的整数记为 ai,j。
网格 a 上一条长度为 k 的路径 p 定义为一个格子序列 p0,p1,…,pk,满足:对每个 0≤i<k,pi 与 pi+1 共享一条边(即相邻),且所有 pi 互不相同。一条长度为 k 的简单路径 p 的转向次数(记为 t(p))定义为满足以下条件的下标 i(其中 1≤i<k)的个数:
- 设 pj=(xj,yj),其中 j∈{i−1,i,i+1},则同时满足 xi−1=xi+1 且 yi−1=yi+1。
路径 p=[(x0,y0),(x1,y1),…,(xk,yk)] 被称为合法路径,当且仅当满足以下全部条件:
- k≥1。
- t(p)≤2。
- ax0,y0=axk,yk,ax0,y0>0,且对每个 1≤i<k,均有 axi,yi=0。
无序格子对 ((u1,v1),(u2,v2)) 被称为可连接的,当且仅当存在一条合法路径 p(长度为 k),使得 p0=(u1,v1) 且 pk=(u2,v2)。
求网格 a 中可连接的格子对的总数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤100, 1≤n⋅m≤2⋅106), representing the dimensions of a.
The i-th of the next n lines contains m integers ai,1,ai,2,…,ai,m (0≤ai,j≤n⋅m), representing the i-th row of a.
It is guaranteed that the sum of n⋅m over all test cases does not exceed 2⋅106.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n≤100,1≤n⋅m≤2⋅106),表示矩阵 a 的维度。
接下来的 n 行中,第 i 行包含 m 个整数 ai,1,ai,2,…,ai,m(0≤ai,j≤n⋅m),表示矩阵 a 的第 i 行。
保证所有测试用例中 n⋅m 的总和不超过 2⋅106。
输出格式
For each test case, output an integer representing the number of connectable pairs in a.
对于每个测试用例,输出一个整数,表示数组 a 中可连接的对数。
输入输出样例
输入#1
7 1 1 1 1 1 0 2 2 3 3 0 3 2 2 2 0 0 2 3 3 0 0 0 1 2 1 0 0 0 5 5 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 4 4 0 0 0 0 1 2 1 2 1 0 0 1 0 0 2 0
输出#1
0 0 3 1 1 34 7
说明/提示
In the first and second test cases, there are no connectable pairs in a.
In the third test case, the connectable pairs are:
- (1,1),(1,2).
- (1,2),(2,2).
- (1,1),(2,2).
In the sixth test case, there are 34 connectable pairs. One of them is (1,1),(3,3). Note that (1,1),(5,5) is not a connectable pair, as any path that begins at (1,1) and ends at (5,5) has more than 2 turns.
在第一和第二个测试用例中,数组 a 中不存在可连接的点对。
在第三个测试用例中,可连接的点对有:
- (1,1),(1,2)。
- (1,2),(2,2)。
- (1,1),(2,2)。
在第六个测试用例中,共有 34 个可连接的点对。其中之一是 (1,1),(3,3)。注意,(1,1),(5,5) 不是一个可连接的点对,因为任何从 (1,1) 出发、终止于 (5,5) 的路径的转弯次数均超过 2 次。
输入解题思路,AI测评打分。不知道怎么写?