Universal Cup Judging System

Universal Cup

Límite de tiempo: 4.0 s Límite de memoria: 1024 MB Puntuación total: 100
Estadísticas

給定一個大小為 $n \times n$ 的棋盤,其 $n^2$ 個格子中的每一個都包含數字 $-1$、$0$ 或 $1$ 其中之一。同時給定四個整數 $A_L, A_D, A_R$ 與 $A_U$。我們希望畫出兩條折線:

  • 一條連接棋盤的左上角與右下角;僅沿著格子的邊界向右與向下移動,
  • 另一條連接棋盤的左下角與右上角,僅向右與向上移動。

這兩條折線將棋盤劃分為四個(可能為空的)區域:左、下、右與上。請檢查是否存在兩條折線,使得這四個區域內的格子總和分別等於 $A_L, A_D, A_R$ 與 $A_U$。對於 $t$ 個獨立的測試資料,請分別輸出 TAK(是)或 NIE(否)。

上圖展示了範例測試中同一個 $5 \times 5$ 的棋盤。

左圖展示了總和為 $(1, 0, -2, 1)$(依 $A_L, A_D, A_R, A_U$ 的順序)的區域劃分。第一條折線可以用單字 RDDDRRDRDR 描述,第二條則可以用 RURUURRUUR 描述(其中 D、U 與 R 分別代表沿著邊界向下、向上與向右移動)。例如,右側區域包含 8 個格子,其總和為 $A_R = 0 - 1 - 1 + 0 - 1 + 0 + 1 + 0 = -2$。

右圖展示了總和為 $(0, 2, -3, 1)$ 的區域劃分。左側區域為空,因此其格子的總和為零。這兩條折線可以用單字 DRRRRDDRDD 與 UUUURURRRR 描述。

同一個棋盤無法被劃分為總和為 $(-2, 0, 1, 1)$ 的區域,亦即 $A_L = -2, A_D = 0, A_R = 1, A_U = 1$。請注意,左圖中出現了相同的數值,但順序不同 —— 順序是重要的。

輸入格式

輸入的第一行包含一個整數 $t$ ($1 \le t \le 100\,000$),代表測試資料的數量。

每組測試資料的第一行包含五個整數 $n, A_L, A_D, A_R, A_U$ ($1 \le n \le 2000; -n^2 \le A_L, A_D, A_R, A_U \le n^2$),代表棋盤的邊長以及四個區域的目標總和。

接下來的 $n$ 行描述棋盤;其中第 $i$ 行包含 $n$ 個整數 $a_{i1}, a_{i2}, \dots, a_{in}$ ($a_{ij} \in \{-1, 0, 1\}$),描述第 $i$ 列。整個棋盤的總和等於 $A_L + A_D + A_R + A_U$。

所有測試資料中 $n^2$ 的總和不超過 $4\,000\,000$。

輸出格式

輸出恰好 $t$ 行 —— 對於每組測試資料,若存在能將棋盤劃分為具有指定總和之區域的折線,則輸出單字 TAK(是),否則輸出單字 NIE(否)。

範例

輸入

5
5 1 0 -2 1
1 0 -1 0 0
1 0 0 1 -1
0 1 -1 0 -1
0 0 0 0 1
-1 0 0 0 0
5 0 2 -3 1
1 0 -1 0 0
1 0 0 1 -1
0 1 -1 0 -1
0 0 0 0 1
-1 0 0 0 0
5 -2 0 1 1
1 0 -1 0 0
1 0 0 1 -1
0 1 -1 0 -1
0 0 0 0 1
-1 0 0 0 0
2 1 -1 0 1
1 0
-1 1
1 0 0 0 0
0

輸出

TAK
TAK
NIE
NIE
TAK

說明

前三個測試資料對應題目敘述中所示的例子。它們包含相同的棋盤,並依序詢問總和 $(A_L, A_D, A_R, A_U)$:

  1. $(1, 0, -2, 1)$,答案為 TAK
  2. $(0, 2, -3, 1)$,答案為 TAK
  3. $(-2, 0, 1, 1)$,答案為 NIE

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.