CF105D.Entertaining Geodetics
省选/NOI-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The maps in the game are divided into square cells called Geo Panels. Some of these panels are painted. We shall assume that the Geo Panels without color are painted the transparent color.
Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent color). Each Geo Symbol is located on one Geo Panel, and each Geo Panel may contain no more than one Geo Symbol.
Geo Symbols can be eliminated. To understand better what happens when a Geo Symbol is eliminated, let us introduce some queue to which we will put the recently eliminated Geo Symbols.
Let's put at the head of the queue a Geo Symbol that was eliminated just now. Next, we will repeat the following operation:
Extract the Geo Symbol from the queue. Look at the color of the panel containing the given Geo Symbol. If it differs from transparent and differs from the color of the Geo Symbol, then all Geo Panels of this color are repainted in the color of the given Geo Symbol (transparent Geo Symbols repaint the Geo Panels transparent). Repainting is executed in an infinite spiral strictly in the following order starting from the panel, which contained the Geo Symbol:

In other words, we select all the panels that need to be repainted and find their numbers in the infinite spiral whose center is placed in the position of the given Geo Symbol. After that, we repaint them in the order of the number's increasing.
If a panel contains another Geo Symbol and this panel is being repainted, then the Geo Symbol is removed from the field and placed at the tail of the queue.
After repainting the Geo Symbol is completely eliminated and the next Geo Symbol is taken from the head of the queue (if there is any) and the process repeats. The process ends if the queue is empty.
See the sample analysis for better understanding.
You know the colors of all the Geo Panels and the location of all the Geo Symbols. Determine the number of repaintings, which will occur if you destroy one of the Geo Symbols.
游戏中的地图被划分为称为“地理方格”(Geo Panels)的正方形单元格。其中一部分方格已被上色。我们假设未着色的地理方格为“透明色”。
此外,地图上还存在所谓“地理符号”(Geo Symbols),它们形似不同颜色(包括透明色)的金字塔。每个地理符号位于一个地理方格上,且每个地理方格至多容纳一个地理符号。
地理符号可以被消除。为了更清楚地理解消除某个地理符号时所发生的过程,我们引入一个队列,用于存放最近被消除的地理符号。
我们将刚刚被消除的地理符号置于队列头部。接着,重复执行以下操作:
- 从队列中取出一个地理符号;
- 观察该地理符号所在地理方格的颜色;
- 若该方格颜色既非透明色,又不同于该地理符号自身的颜色,则将所有具有该方格颜色的地理方格重新涂成该地理符号的颜色(透明色的地理符号会将对应方格重涂为透明色)。重涂操作按如下严格定义的无限螺旋顺序进行,起始位置即为该地理符号原先所在的方格:

换言之,我们首先选出所有需要重涂的方格,再以该地理符号原位置为中心,计算这些方格在无限螺旋序列中的编号(编号从中心开始递增)。随后,按编号从小到大的顺序依次重涂这些方格。
若某一方格上存在另一个地理符号,且该方格正处于被重涂过程中,则该地理符号将被移出地图,并添加至队列尾部。
完成重涂后,该地理符号即被彻底消除;接着,若队列非空,则从队列头部取出下一个地理符号,重复上述过程。当队列为空时,整个过程结束。
参见样例分析以加深理解。
已知所有地理方格的颜色以及所有地理符号的位置,请确定:若摧毁其中某一个地理符号,总共会发生多少次重涂操作。
输入格式
The first line contains two integers n and m (1 ≤ n, m ≤ 300) — the height and the width of the map (in cells).
Then follow n line; each of them contains m numbers — the Geo Panels' colors.
Then follow n more lines; each of them contains m numbers — the Geo Symbols' description. -1 means that the given position contains no Geo Symbol. Otherwise, the number represents the color of the Geo Symbol in the given position.
All colors are integers from 0 to 109. 0 represents the transparent color.
The last line contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column where the Geo Symbol is placed that needs to be eliminated. The rows are numbered from top to bottom, the columns are numbered from left to right. Coordinates are 1-based. It is guaranteed that the position with coordinates (x, y) contains a Geo Symbol.
第一行包含两个整数 n 和 m(1≤n,m≤300)—— 地图的高度和宽度(以格子为单位)。
接下来是 n 行,每行包含 m 个整数 —— 表示地理面板(Geo Panels)的颜色。
再接下来是 n 行,每行包含 m 个整数 —— 表示地理符号(Geo Symbols)的描述:−1 表示该位置没有地理符号;否则,该数字表示该位置地理符号的颜色。
所有颜色均为 0 到 109 之间的整数。0 表示透明色。
最后一行包含两个整数 x 和 y(1≤x≤n,1≤y≤m)—— 表示需要消除的地理符号所在的行与列。行号从上到下编号,列号从左到右编号,坐标采用 1-based 编号。保证坐标 (x,y) 处确实存在一个地理符号。
输出格式
Print the single number — the total number of repaintings after the Geo Symbol is eliminated.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (you may also use the %I64d specificator).
输出一个整数——即在消除几何符号(Geo Symbol)后,总共需要重新涂色的次数。
请注意,在 C++ 中请勿使用 %lld 格式说明符来读取或写入 64 位整数。推荐使用 cout 流(您也可以使用 %I64d 格式说明符)。
输入输出样例
输入#1
5 5 9 0 1 1 0 0 0 3 2 0 1 1 1 3 0 1 1 1 3 0 0 1 2 0 3 -1 1 -1 3 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1 2 3 -1 -1 -1 -1 -1 -1 2 4 2
输出#1
35
说明/提示
All actions of the sample you can see on the following picture:

If your browser does not support APNG and you see just static image, you can see GIF version of this image by the following link:
http://assets.codeforces.com/images/geo_slow.gif
你可以在下图中看到示例的所有操作:

如果你的浏览器不支持 APNG,仅显示静态图像,则可通过以下链接查看该图像的 GIF 版本:
http://assets.codeforces.com/images/geo_slow.gif
输入解题思路,AI测评打分。不知道怎么写?