2031: [USACO 2025 January Contest, Bronze] Problem 1. Astral Superposition

文件提交:无需freopen 内存限制:512 MB 时间限制:16.000 S
评测方式:普通裁判 命题人:
提交:1 解决:1

题目描述

Bessie is using her nifty telescope to take photos of all the stars in the night sky. Her telescope can capture an N×N (1≤N≤1000) photo of the stars where each pixel is either a star or empty sky. Each star will be represented by exactly one pixel, and no two distinct stars share the same pixel. 

Overnight, something strange happens to the stars in the sky. Every star either disappears or moves A pixels to the right, and B pixels downwards (0≤A,B≤N). If a star disappears or moves beyond the photo boundary, it no longer appears in the second photo. 

Bessie took photos before and after the shifting positions, but after experimenting in Mootoshop, she accidentally superimposed one photo onto the other. Now, she can see white pixels where both photos were empty, gray pixels where stars existed in exactly one photo, and black pixels where there was a star in both photos. Bessie also remembers that no new stars moved into the frame of the second photo, so her first photo contains all of the stars in the night sky. 

Given the final photo, determine the minimum possible number of stars in the sky before the shifting incident for T (1≤T≤1000) independent test cases. If no arrangement of stars can produce the given final photo, output −1.

输入

The first line of input contains T and T test cases will follow. 

The first line of each test case will contain N A B

Then follow N lines each representing one row of the superimposed photo. The th row from the top is represented by a string ci,1 ci,2… ci,N, where each ci,j ∈ {W,G,B }, representing the colors white, gray, and black respectively.

It is guaranteed that the sum of N2 over all test cases will not exceed 107.

输出

For each test case, output the minimum number of stars that existed before the shifting, or −1 if impossible.

样例输入

1
3 0 0
WWB
BBB
GGG

样例输出

7

提示

In this example, there is no shifting. The first photo is as follows: (. for sky, * for star)

 ..* 

*** 

*** 

The second photo, where the stars on the bottom row disappeared, is as follows:

. . * 

***

. . . 

This is the only way to produce the superimposed photo, so the minimum possible number of initial stars is 7.


Sample Input 2:

3

5 1 2

GWGWW

WGWWW

WBWGW 

WWWWW 

WWGWW

3 1 1 

WWW 

WBW 

WWW 

3 1 0 

GGB 

GGW 

WWW


Sample Output 2

4

-1

4


In the first case, there were at least  stars at the start. If we let  denote the intersection of the th row from the top and th column from the left, one possibility is that they were initially at  and . All the stars shifted, except for the one at  which disappeared.

In the second case, there is no arrangement of stars in the initial photo that can produce the middle black pixel given the shift.

In the third case, there were at least  stars at the start. One possibility is that they were initially at  and . In the second photo, the star originally at  disappeared and the star originally at  moved off frame. The other two stars shifted to the right by 1.


Scoring:
  • Input 3: 
  • Inputs 4-7: 
  • Inputs 8-9: 
  • Inputs 10-12: No additional constraints.




来源/分类