QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#23928. 填色

Statistics

題目描述

JOI 君緊係度玩畫圖軟件。

喺畫圖軟件入面,可以喺直 $H$ 行、橫 $W$ 列嘅長方形格仔紙上面畫畫。每個格仔都有設定好嘅顏色,顏色用 $1$ 以上 $10^9$ 以下嘅整數表示。

由上至下第 $i$ 行($1 \le i \le H$)、由左至右第 $j$ 列($1 \le j \le W$)嘅格仔稱為格仔 $(i,j)$。而家,格仔 $(i,j)$ 嘅顏色係 $A_{i,j}$。

由格仔 $(i,j)$ 出發,重複移動去有公共邊嘅相鄰格仔,而且唔進入任何同格仔 $(i,j)$ 顏色唔同嘅格仔所能去到嘅格仔集合,喺度稱為格仔 $(i,j)$ 嘅區域。

畫圖軟件有一個叫做填色嘅功能。喺呢個功能入面,指定某個格仔 $(x,y)$($1 \le x \le H$,$1 \le y \le W$)同顏色 $c$($1 \le c \le 10^9$)之後,包含喺格仔 $(x,y)$ 嘅區域入面嘅所有格仔顏色都會變成 $c$。

JOI 君揀咗某個格仔 $(x,y)$ 同顏色 $c$,指定呢個格仔同顏色並啱啱好使用 $1$ 次填色。用咗填色之後,包含喺格仔 $(x,y)$ 嘅區域入面嘅格仔數量就係 JOI 君嘅得分。

請編寫程式求出 JOI 君可以達到嘅最高得分。

輸入格式

輸入按以下格式畀出:

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
:
A_{H,1} A_{H,2} \ldots A_{H,W}

輸出格式

喺 $1$ 行內輸出 JOI 君可以達到嘅最高得分。

數據範圍

  • $1 \le H \le 500$。
  • $1 \le W \le 500$。
  • $1 \le A_{i,j} \le 10^9$($1 \le i \le H$,$1 \le j \le W$)。
  • 輸入嘅數值全部都係整數。

子任務

  1. ($9$ 分)$H = 1$。
  2. ($32$ 分)$H \le 30$,$W \le 30$,$A_{i,j} \le 5$($1 \le i \le H$,$1 \le j \le W$)。
  3. ($18$ 分)$H \le 30$,$W \le 30$。
  4. ($10$ 分)$A_{i,j} \le 2$($1 \le i \le H$,$1 \le j \le W$)。
  5. ($31$ 分)無額外限制。

樣例

樣例輸入 1

4 4
1 2 3 1
2 2 3 1
1 2 3 1
3 3 2 2

樣例輸出 1

9

最開始嘅時候,包含喺格仔 $(2,2)$ 嘅區域入面嘅格仔有格仔 $(1,2), (2,1), (2,2), (3,2)$ 共 $4$ 個。因此,如果指定格仔 $(2,2)$ 同顏色 $3$ 去用填色,好似下圖咁,呢 $4$ 個格仔嘅顏色會變成 $3$。

problem_23928_f670918b4b8047ebeaf9bad79de947bd.png

用咗填色之後,包含喺格仔 $(2,2)$ 嘅區域入面嘅格仔變成格仔 $(1,2), (1,3), (2,1), (2,2), (2,3), (3,2), (3,3), (4,1), (4,2)$ 共 $9$ 個。因此,JOI 君嘅得分會變成 $9$。

因為 JOI 君嘅得分冇辦法達到 $10$ 或以上,所以輸出 $9$。

呢個輸入滿足子任務 $2, 3, 5$ 嘅限制。


樣例輸入 2

2 10
1 2 2 1 3 3 3 3 1 1
1 1 1 1 1 1 1 3 3 3

樣例輸出 2

18

呢個輸入滿足子任務 $2, 3, 5$ 嘅限制。


樣例輸入 3

5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

樣例輸出 3

25

呢個輸入滿足子任務 $2, 3, 4, 5$ 嘅限制。

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.