#P1111A. 哈!昨日五次重现(简单版)
哈!昨日五次重现(简单版)
L. Ha, It's Yesterday Five Times More(Easy Version)
The only difference between the easy version and the hard version is that, in the easy version, , , , and exactly one of and is satisfied.
Background
In 2018, 2020, 2021, 2022, and 2023, the SUA problem-setting team proposed five problems related to kangaroos in the ICPC Nanjing Site.
To warm up before the Jinan Site, pzr decided to solve the following problem about kangaroos. Can you help him?
Description
Given an grid, initially, each cell can have at most one kangaroo (but during the operation, a cell may contain multiple kangaroos). In addition, there's a hole in a specific cell .
You can perform several operations, each of which is one of the following:
- Cost , causing all kangaroos to gather with the hole as the center, specifically:
- All kangaroos in the cells to the left of the hole (in the same row but with column number less than the column number of the hole) move one cell to the right.
- All kangaroos in the cells to the right of the hole (in the same row but with column number greater than the column number of the hole) move one cell to the left.
- All kangaroos in the cells above the hole (in the same column but with row number less than the row number of the hole) move one cell down.
- All kangaroos in the cells below the hole (in the same column but with row number greater than the row number of the hole) move one cell up.
- All kangaroos in the cells to the top-left of the hole (with row number less than the row number of the hole and column number less than the column number of the hole) move one cell to the right and then one cell down.
- All kangaroos in the cells to the bottom-left of the hole (with row number greater than the row number of the hole and column number less than the column number of the hole) move one cell to the right and then one cell up.
- All kangaroos in the cells to the top-right of the hole (with row number less than the row number of the hole and column number greater than the column number of the hole) move one cell to the left and then one cell down.
- All kangaroos in the cells to the bottom-right of the hole (with row number greater than the row number of the hole and column number greater than the column number of the hole) move one cell to the left and then one cell up.
- Cost , choosing a specific kangaroo.
- Move it to one of the four adjacent cells (up, down, left, right).
When a kangaroo falls into the hole, it will be removed from the grid. If after a series of operations, exactly one kangaroo remains in the grid, it becomes the final winner.
Now, assuming you are a kangaroo at position , please calculate: What is the minimum cost to become the final winner? For each kangaroo, find the answers . To avoid excessive output, you only need to find the sum of the squares of the individual answers, i.e., .
Input Format
The first line contains an integer , representing the number of test cases.
For each test case:
The first line contains five integers , representing the number of rows and columns of the grid, the cost of the first operation, and the position of the hole.
Next, there are lines, each containing a string of length , consisting of only or , indicating the initial number of kangaroos in that cell. It's guaranteed that there are no kangaroos at the hole's initial position. It's guaranteed that there is at least one kangaroo in the entire grid.
Output Format
For each test case: Only one non-negative integer, representing the sum of the squares of the minimum costs for each kangaroo.
Sample Input 1
2
3 4 0 1 1
0100
1010
0100
3 4 1000 1 1
0100
1010
0100
Sample Output 1
10
148
Sample 1 Explanation
For the first test case, the cost of aggregation operations is zero, and the hole is located at the top-left corner (1,1).
For the kangaroo at (1,2), it needs to move right by one step.
0010 1010 0100
Next, all kangaroos are gathered towards the hole.
0100 1200 0000
The kangaroo at (2,1) falls into the hole, and the kangaroos at (2,3) and (3,2) gather at (2,2).
At this point, it moves right by one step again.
0010 1200 0000
Next, all kangaroos are gathered towards the hole.
0100 0000 0000
The total cost is .
Similarly, the total cost at (2,1) is , at (2,3) is , and at (3,2) is .
Hence, the output is .
Data Range and Constraints
Subtask 1 (50 points):
Subtask 2 (50 points):
For all test cases:
,。
,
Exactly one of and is satisfied.
It's guaranteed that there are no kangaroos initially at the hole's position. It's guaranteed that there is at least one kangaroo in the entire grid.
It's guaranteed that the sum of in all test cases does not exceed .
It can be proved that under the current constraints, any kangaroo can become the final winner.
It can be proved that under the current constraints, the answer must be representable by a -bit integer.
The English Version of the Statement is Translated By ChatGPT.
相关
在下列比赛中: