300553: CF105D. Entertaining Geodetics

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Entertaining Geodetics

题意翻译

**描述** 在此游戏中地图被分为了许多叫作Geo格的正方形方格,其中一些被涂上色,假设没有涂色的为透明色。   地图中还有些Geo符号,它们样子像不同颜色的金字塔(包括透明Geo符号)。每个Geo符号都坐落在Geo格上,每个Geo格上最多一个Geo符号。   Geo符号可以被消除。为了更好地理解Geo符号在消除时发生了什么,这里引入把刚消除的Geo符号放入的队列。   从队列中取出Geo符号,观察包含Geo符号的Geo格的颜色,如果它不是透明的且颜色不同于Geo符号,则把所有这个颜色的Geo格重新涂为Geo符号的颜色(透明的Geo符号则为透明色)。重涂色是在一个无限大的区域从那个有符号的Geo格子开始螺旋状进行的。换句话说,我们选择所有需要重涂色的方格找到它们在以有符号格为中心的无限螺旋表格中所对应的数字。此后按数字的增加顺序我们对其重染色。   如果在重染色时遇到一个格子包含另一个Geo符号的情况则将Geo符号移出并放置在队列尾部。   当重染色结束后Geo符号彻底消失,并且队列中下一个Geo符号(如果有)将取出,重复此操作直至队列为空。   为了更好地理解请看一个例子。   你知道所有格子的颜色、所有符号的位置。计算出队列里符号彻底消失时所造成的重染色次数。   推荐使用I64d输出。 **输入描述:**   第一行包含两个数n,m(1<=n,m<=300)—地图的高和宽。   接下来n行每行m个数—格子的颜色。   接下来n行每行m个数—对符号的描述,-1表示没有符号,否则数字代表符号的颜色。   所有颜色都是属于0到10^9的整数,0表示透明。   最后一行两个数x,y(1<=x<=n,1<=y<=m)—需要消除的Geo符号的行和列位置。行从上到下标记,列从左往右标记,从1开始。保证位置(x,y)包含一个符号。 **输出描述:**   一行一个数—符号消除时重染色次数。

题目描述

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: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF105D/430d2dc39e8c5cda16cf52fdf0302368ccd24c75.png)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.

输入输出格式

输入格式


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 $ 10^{9} $ . $ 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.

输出格式


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).

输入输出样例

输入样例 #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: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF105D/45edbaa93b41bf71ed92bf8c850ca560634e9031.png) 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

Input

题意翻译

**描述** 在此游戏中地图被分为了许多叫作Geo格的正方形方格,其中一些被涂上色,假设没有涂色的为透明色。   地图中还有些Geo符号,它们样子像不同颜色的金字塔(包括透明Geo符号)。每个Geo符号都坐落在Geo格上,每个Geo格上最多一个Geo符号。   Geo符号可以被消除。为了更好地理解Geo符号在消除时发生了什么,这里引入把刚消除的Geo符号放入的队列。   从队列中取出Geo符号,观察包含Geo符号的Geo格的颜色,如果它不是透明的且颜色不同于Geo符号,则把所有这个颜色的Geo格重新涂为Geo符号的颜色(透明的Geo符号则为透明色)。重涂色是在一个无限大的区域从那个有符号的Geo格子开始螺旋状进行的。换句话说,我们选择所有需要重涂色的方格找到它们在以有符号格为中心的无限螺旋表格中所对应的数字。此后按数字的增加顺序我们对其重染色。   如果在重染色时遇到一个格子包含另一个Geo符号的情况则将Geo符号移出并放置在队列尾部。   当重染色结束后Geo符号彻底消失,并且队列中下一个Geo符号(如果有)将取出,重复此操作直至队列为空。   为了更好地理解请看一个例子。   你知道所有格子的颜色、所有符号的位置。计算出队列里符号彻底消失时所造成的重染色次数。   推荐使用I64d输出。 **输入描述:**   第一行包含两个数n,m(1<=n,m<=300)—地图的高和宽。   接下来n行每行m个数—格子的颜色。   接下来n行每行m个数—对符号的描述,-1表示没有符号,否则数字代表符号的颜色。   所有颜色都是属于0到10^9的整数,0表示透明。   最后一行两个数x,y(1<=x<=n,1<=y<=m)—需要消除的Geo符号的行和列位置。行从上到下标记,列从左往右标记,从1开始。保证位置(x,y)包含一个符号。 **输出描述:**   一行一个数—符号消除时重染色次数。

加入题单

上一题 下一题 算法标签: