We are given a star map of size n x m (n,m up to 300). There are stars on some squares. A constellation of the Cross is defined by 5 stars such that for some integer x (radius), there is a central star (1st) and then four stars placed at distance x up, down, left, right from the central star. The positions:
- 2nd: same column, row = row1 - x (up)
- 3rd: same column, row = row1 + x (down)
- 4th: same row, col = col1 - x (left)
- 5th: same row, col = col1 + x (right)
Thus all five squares must contain stars. The radius x must be integer ≥? Actually x must be such that these positions are within the map? The problem statement: "the 2nd is on the same vertical line as the 1st, but x squares up" and similarly. Typically, it implies that the positions must exist on the map, i.e., the coordinates must be valid (between 1 and n, 1 and m). Also, x must be at least 1? Possibly x could be 0? But then the other stars would coincide with the central star. Usually such constellations would require distinct stars, so x>0. The problem statement says "any 5 stars so, that for some integer x (radius of the constellation) the following is true: ...". It doesn't explicitly forbid x=0, but having the same star multiple times is not possible because there is at most one star per square, and they are distinct stars. So x must be at least 1. Also, the constellation must consist of exactly 5 stars; the same star cannot be used twice. So x>0 and all positions must be within bounds and contain stars.
We need to number all such constellations (all valid (center, x) pairs satisfying conditions) according to a lexicographic-like ordering: first by radius x (ascending), then if radii equal, by the row of the central star (ascending? "the one, whose central star if higher than the central star of the other one" – higher means smaller row number? Since coordinates: upper-left corner is (1,1). So "higher" means smaller row index. So we order by row asc (higher first). If same row, then by column asc (left first). So ordering key: (radius x, center row, center col). Note: "the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one." So yes: sort by x asc, then row asc, then col asc.
We need to find the k-th constellation in this order. If fewer than k constellations, output -1.
Constraints: n,m ≤ 300 → total cells up to 90k. k up to 3e7, which is high. The number of possible constellations could be large: For each cell as center, for each possible radius up to max distance to borders, if all four positions have stars, it's a valid cross. In worst case, if all cells are stars, number of crosses: For each cell (nm) and each radius such that all four arms within bounds, that radius ≤ min(r-1, n-r, c-1, m-c). So number is sum over centers of that min. This can be up to O(nmmin(n,m)). For 300x300, maximum min about 150, product 90k * 150 = 13.5 million, maybe up to a bit more if n and m balanced. Actually for a full star grid, the number of crosses is sum_{i=1}^{n} sum_{j=1}^{m} min(i-1, n-i, j-1, m-j). For n=m=300, it might be around something like O(n^3)?? Let's approximate: The average min across the grid is about n/4 maybe? So total ~ (n^2)(n/4) = n^3/4 = 27e6 /4? Actually n=300, n^3=27e6, /4 = 6.75e6. Could be up to maybe 7 million. But k up to 30 million, so maybe there could be that many? But maximum possible number might be less than 30 million. For n=m=300, full star grid, we can compute approximate: The maximum radius for center (i,j) is min(i-1, n-i, j-1, m-j). Sum over all i,j of that min. For symmetric, we can compute: sum_{i=1}^{n} sum_{j=1}^{m} min(i-1, n-i, j-1, m-j). For n=300,m=300, all distances symmetric. This is essentially sum over i,j of min(i-1, 300-i, j-1, 300-j). i-1 and 300-i: the min of those is the distance to nearest top/bottom boundary, call d_vert = min(i-1, n-i). Similarly d_horiz = min(j-1, m-j). Then min of both? Actually the radius is limited by all four directions: up: i-1, down: n-i, left: j-1, right: m-j. The maximum possible x is min(up, down, left, right). That is min(d_vert, d_horiz). So radius max = min(d_vert, d_horiz). So for each cell, the number of possible x values is exactly that min (since x can be from 1 to that min). So total crosses = sum over cells of min(d_vert, d_horiz). For n=m=300, that is sum_{i=1}^{300} sum_{j=1}^{300} min(d_vert(i), d_horiz(j)). Where d_vert(i)=min(i-1, 300-i), d_horiz(j)=min(j-1, 300-j). The values range from 0 to 149 (since for i=1, min(0,299)=0; i=300,0; maximum at i=150,151 give 149? Actually for i=150: i-1=149, 300-i=150 => min=149; i=151: 150,149 => min=149). So max 149. So total crosses = sum_{d1=0..149} sum_{d2=0..149} count(i: d_vert=d1)count(j: d_horiz=d2) min(d1,d2). The counts for each distance: For n even? 300 is even, so distances: i=1:0, i=2:1? Actually careful: For n=300, d_vert(i)=min(i-1,300-i). This yields symmetric distribution: For each i from 1 to 300, d_vert ranges from 0 to 149. How many i give d_vert = t? For t from 0 to 149, number of i such that min(i-1,300-i)=t. For t=0: i=1 and i=300 ->2 cells. For t=1: i=2 and i=299 ->2 cells? Check i=2: i-1=1, 300-2=298 -> min=1; i=299: 298,1->1. Also i=??? Could there be others? Actually the condition min(i-1,300-i)=t means either i-1 = t and i-1 <= 300-i => i = t+1, and t+1 <= 300 - (t+1) => 2t+2 <=300 => t <=149; or 300-i = t and 300-i <= i-1 => i=300-t, and 300-t-1? Let's derive properly. Usually for a line of length n, number of positions with distance to nearest edge = d is: 2 for d from 1 to floor((n-1)/2) if n odd? For n even, there are two central rows that give max d = n/2 -1? Actually standard: distance to nearest border = min(i-1, n-i). For n even, the maximum value is (n/2 -1) when i=n/2 or n/2+1? For n=300, max =149 as we said. The number of indices i with d = t for t=1,...,149 is 2 (one from left side: i = t+1, one from right side: i = n-t). For t=0, there are also 2: i=1 and i=n. So distribution: each d from 0 to 149 appears exactly 2 times. Check t=149: i=150 gives d=149 (since 150-1=149,300-150=150 -> min=149) and i=151 gives 151-1=150,300-151=149 -> min=149. So indeed 2 each. So total i count = 300, sum of 2*150 =300. Good.
Similarly for columns j same distribution: each d from 0 to 149 appears twice.
Thus total crosses = sum_{t1=0}^{149} sum_{t2=0}^{149} 22 * min(t1, t2) = 4 * sum_{t1=0}^{149} sum_{t2=0}^{149} min(t1,t2). We can compute that sum. For a range 0..L with L=149, sum_{a=0}^{L} sum_{b=0}^{L} min(a,b) =? Known: sum_{i=0}^{L} sum_{j=0}^{L} min(i,j) = (L(L+1)(2L+1))/3? Let's derive. For non-negative integers, min(i,j) symmetric. Equivalent to sum_{i=0}^{L} sum_{j=0}^{L} min(i,j) = sum_{i=0}^{L} ( i(i+1)/2? Actually we can compute: For fixed i, sum_{j=0}^{L} min(i,j) = sum_{j=0}^{i} j + sum_{j=i+1}^{L} i = (i(i+1)/2) + i*(L - i). = i(i+1)/2 + iL - i^2 = iL + i(i+1)/2 - i^2 = iL + (i^2 + i - 2i^2)/2 = iL + (-i^2 + i)/2 = iL - i(i-1)/2. Then sum over i from 0 to L: sum_{i=0}^{L} (iL - i(i-1)/2). For i=0, term 0. So total = L * sum i - (1/2) sum i(i-1). Sum i = L(L+1)/2. Sum i(i-1) = sum (i^2 - i) = sum i^2 - sum i = L(L+1)(2L+1)/6 - L(L+1)/2 = L(L+1)[(2L+1)/6 - 1/2] = L(L+1)[(2L+1 - 3)/6] = L(L+1)(2L-2)/6 = L(L+1)(L-1)/3. So (1/2) sum i(i-1) = L(L+1)(L-1)/6. Then total = L * [L(L+1)/2] - L(L+1)(L-1)/6 = L^2(L+1)/2 - L(L+1)(L-1)/6 = L(L+1)[ L/2 - (L-1)/6 ] = L(L+1)[ (3L - (L-1))/6 ] = L(L+1)( (2L+1) )/6? Let's compute: 3L - (L-1) = 2L+1. So total = L(L+1)(2L+1)/6. Wait that's the formula for sum of squares? Actually sum of i^2 = L(L+1)(2L+1)/6. That's interesting: sum_{i,j} min(i,j) for i,j from 0 to L equals L(L+1)(2L+1)/6. But we need to check with small L to verify. For L=1 (0,1): min: (0,0)=0, (0,1)=0, (1,0)=0, (1,1)=1 => total 1. The formula L(L+1)(2L+1)/6 with L=1: 123/6=1. Good. For L=2 (0,1,2): manually? So seems correct.
Thus sum_{t1=0}^{L} sum_{t2=0}^{L} min(t1,t2) = L(L+1)(2L+1)/6.
With L=149: 149150299/6 = compute: 149150=22350, 22350299 = 22350300 - 22350 = 6,705,000 - 22,350 = 6,682,650. Divide by 6 = 1,113,775? Actually 6,682,650 /6 = 1,113,775 exactly? 6,682,650/6 = 1,113,775 (since 61,113,775 = 6,682,650). So sum = 1,113,775. Then total crosses = 4 * 1,113,775 = 4,455,100. So maximum crosses for full star grid is about 4.46 million, which is less than 3e7. However, note that k can be up to 3e7, but maximum total crosses is <5 million for n,m=300. So if k is larger than that, we output -1. But the problem statement: k up to 3e7, but actual constellations less than that. So we should compute total number and compare with k.
But note that the grid is not necessarily full; many stars missing will reduce number of crosses.
Our task: find k-th cross in the ordering by (x, row, col). Since total crosses max ~4.5 million, we can in principle generate all crosses, sort them according to that ordering, then pick k-th. However, careful: n,m up to 300, number of cells ~90k; but potential crosses: each star as center, each radius up to its limit; maximum number per center ~150, so total cross count max ~4.5 million. This is manageable in memory and time? 4.5 million objects, each storing (x, row, col) maybe 3 ints, that's about 4.5M * 12 bytes = 54MB, plus overhead if using Python list of tuples maybe larger. In Python, 4.5 million tuples of three ints could be memory heavy (each tuple overhead ~56 bytes plus int objects 28 bytes each? That would be huge). So we need a smarter approach.
But maybe we can generate crosses and process them on the fly with counting and selection using something like nth_element? However, we need to output the k-th one in sorted order, not just count.
We could consider counting how many crosses have radius <= some value, and then binary search on radius to find the radius where k-th cross lies. Then within that radius, we can enumerate crosses with that exact radius in row, col order and pick the appropriate one.
Because the ordering sorts first by radius x. So all crosses with radius < r come before crosses with radius = r. So we can compute the total count of crosses with radius less than a given value. Then we can find the radius r such that cumulative count before r < k <= cumulative count up to r. Then we need the (k - count_before)th cross among those with radius = r, sorted by (row, col) ascending.
Thus we need to compute for each possible radius value (x from 1 to max possible) the list of centers (row, col) that support a cross of radius x (i.e., all four positions at distance x from center have stars). Then these crosses for a given x are sorted by row, then col.
We can precompute for each cell whether there is a star. Then for each possible center, we can compute the maximum x such that up/down/left/right all have stars. However, that maximum x might be limited by both the distances to borders and presence of stars. But we need to know for each x, which centers work.
Brute force: For each center (i,j) that is a star itself (since central star must be present), we can test each x from 1 to some max, checking if the four offset positions are within bounds and have stars. The maximum possible x for a center is min(i-1, n-i, j-1, m-j) because those are border limits. But also need stars at those positions. So we could for each center compute all valid x by scanning outward until one of the arms missing. Since max distance per center up to 150, total checks per center up to 150, and there could be up to 90k centers (if all stars). So total checks up to ~13.5 million, which is okay. Then we can for each valid x, add the center to a list for that x.
However, we must consider that a cross requires central star and four arm stars. The central star must be present. So we only consider cells that are stars as potential centers.
So algorithm:
- Read n,m,k.
- Read grid, store as list of strings or boolean matrix.
- For each cell (i,j) where grid[i][j] == '*', compute the maximum possible radius based on border distances: maxX_border = min(i-1, n-i, j-1, m-j). But we also need to check if the arm positions have stars. So we could for x from 1 to maxX_border:
- check if grid[i-x][j]=='' and grid[i+x][j]=='' and grid[i][j-x]=='' and grid[i][j+x]==''.
- If any fails, break? Actually not necessarily: if at a certain x fails, larger x might also fail because the same positions or further ones must be stars. But if grid[i-x][j] is not star, then for any larger x, that position is still needed? Actually for larger x, the up star is at i-x' where x'>x, which is farther up, so it's different cell. So it's possible that for x=2 fails because up-2 missing but for x=3 up-3 might be star. So we cannot break early; we must check each x independently. However, we can precompute arrays to quickly check for each center the presence of consecutive stars in each direction, maybe using prefix sums or something. But with n,m=300, per center we can loop up to ~150 and check O(1) each, total about up to 90k * 150 = 13.5M checks, each check involves 4 lookups, so about 54M operations, which is fine in Python if optimized (e.g., using list of strings and direct indexing). 54M maybe borderline but likely okay given constraints (300x300 = 90k centers only if all stars; but many cells may be '.' reducing number). So we can do direct checks.
But we also need to store for each radius x the list of centers (i,j) that have a cross of radius x. Since x can range up to maybe 149 (max border radius). Actually the maximum possible x overall is floor((min(n,m)-1)/2) maybe? Because for a cross to fit, need at least x in each direction, so center must be at least x+1 from edges, so max x <= floor(min(n,m)-1)/2? For n,m=300, max x = 149. So radius values up to 149. That's small. So we can have an array of lists: crosses_by_radius = [[] for _ in range(maxR+1)] where maxR = maybe min(n,m)//2? Actually careful: if n=1 or m=1, no cross because need up/down/left/right, so radius must be at least 1 and need two rows and two columns. So max possible x = min( (n-1)//2? Actually condition: i - x >=1, i + x <= n, j - x >=1, j + x <= m. So i >= x+1, i <= n-x, j >= x+1, j <= m-x. So maximum x is floor(min(n,m)-1? Actually for a center to exist, there must be at least one cell satisfying those inequalities, which requires x <= (n-1)/2 and x <= (m-1)/2. So maxX = floor(min(n-1, m-1)/2). For n=m=300, maxX = floor(299/2)=149. So radius range 1..149.
Thus we can allocate an array of size maxX+1 (maybe up to 150). For each center that is a star, for each x from 1 to maxX_center (border limit) check conditions and if true, add center to list for that x.
But careful: border limit for center is min(i-1, n-i, j-1, m-j). But also the cross requires stars at the four positions, which might not be there. So we check each x.
After building lists, we can compute total crosses = sum over x len(crosses_by_radius[x]). Compare with k; if k > total, output -1.
Else, we need to find k-th.
Because radius ordering is ascending, we can iterate over x from 1 to maxX, and maintain cumulative count. For each x, the crosses of that radius need to be sorted by row asc, then col asc. Since we are building lists as we go, we can sort each list after all additions (or we could insert in sorted order but easier to sort later). Because total number of crosses across all radii is at most ~4.5 million. Sorting each list individually: each list size? For a given radius x, how many centers could have cross of radius exactly x? For full star grid, distribution? Possibly many. The sum of sizes of all lists is total crosses. Sorting each list separately could be done; but we need to sort by row then col. Since the lists may be large for small radii? Actually for small radii, many centers could have cross of that radius. For radius 1, any star with all four neighbors also stars. In a full star grid, all interior stars with i from 2..n-1 and j from 2..m-1 would have radius 1 cross, that's (n-2)(m-2) = 298298 = 88,804 for n=m=300. That's large but manageable to sort 88k items. For radius 2, centers need all positions at distance 2, which reduces count. Overall total items across lists is 4.5M, so sorting each list individually might be okay if we use Python's Timsort which is O(N log N). Sorting a single list of 88k items is fine. Sorting all lists cumulative complexity would be sum over x of (size_x * log size_x). This should be acceptable for 4.5M total items; worst-case if many items are in a few lists: e.g., radius 1 has ~89k, radius 2 maybe ~? For full grid, the count of crosses for radius x is number of centers where min(d_vert, d_horiz) >= x, and also need all four arm stars, which in full grid are always present, so count_x = number of centers (i,j) such that d_vert(i) >= x and d_horiz(j) >= x. Since d_vert(i) >= x means i in [x+1, n-x] and similarly for j. So count_x = (n - 2x)(m - 2x) for x up to floor(min(n,m)/2). For n=m=300, count_1 = (300-2)(300-2)=298^2=88804, count_2=296^2=87616, ... up to count_149 = (300-298)(300-298)=2^2=4. So total sum of count_x for x=1..149 is sum_{x=1}^{149} (300-2x)^2. That sum indeed equals about 4.45 million. Sorting each list: each list would be count_x items. Sorting each list of size (300-2x)^2 might be done, but note the list for radius 1 is 88k, radius 2 is 87.6k, etc. Sorting 149 lists each of size decreasing from 88k to 4. That's total sorting cost: sum_{x} N_x log N_x. The largest N_x is about 88k, log2(88k)~16.6, so cost ~88k16.6 = 1.46M operations per sort; over 149 lists, dominated by first few. Rough estimate: sum_{x=1}^{149} (300-2x)^2 * log2((300-2x)^2). This might be maybe tens of millions, which is okay in Python? 88k * 17 = 1.5M, then next 86k~17=1.46M, etc. Summing 75 such? Actually after x=10, (300-20)=280 => N=78400, log ~16.3, product ~1.28M. Summing for 149 terms, but many later terms are small. The sum of products maybe around 50-80 million comparisons. That's okay. However, Python sorting of tuples may be a bit heavy but still likely within time limit if well-implemented? Let's test: 4.5 million items total, each sorting a list of up to 88k items. Sorting 88k list of tuples (two ints) in Python takes maybe ~0.02-0.03 seconds? Actually sorting 100k integers takes about 0.02 sec? Sorting 100k tuples of two ints maybe 0.03-0.04 sec. 149 sorts might be like 0.04149 ≈ 6 seconds, which might be borderline but could be okay if optimized? But we also have the generation step. However, we need to consider that input size is small, but time limits typical (1-2 seconds)? Not sure. We need a more efficient approach to avoid sorting many lists.
Alternative: Since we only need the k-th cross overall, we could generate all crosses (with radius, row, col) into a single list, then sort by (radius, row, col). But that would be 4.5M items; sorting such a list in Python would be heavy: 4.5M * log2(4.5M) ≈ 4.5M * 22 ≈ 100M comparisons, each comparing tuples (three ints) maybe okay but memory might be high (each tuple ~72 bytes, total 324MB). Probably too much.
Thus we should adopt the radius grouping method, but maybe we can avoid sorting per radius by generating the centers in the required order on the fly.
Observing that for a given radius x, the ordering is by row asc, then col asc. If we can generate centers that satisfy the cross condition in that order directly, we could just count them and when we hit the k-th we output.
One approach: For each radius x, we could iterate over rows i from x+1 to n-x, and for each row, iterate over columns j from x+1 to m-x, and check if (i,j) is a star and the four offset positions have stars. If yes, then we have a cross. If we do that for radii in increasing order, we are generating crosses exactly in the required order (since rows increase, columns increase within each row). But careful: The ordering across radii: we process radii from smallest to largest. Within a given radius, we process rows from smallest to largest, and within a row, columns from smallest to largest. That matches the ordering exactly. So we can simply simulate enumeration in order, counting as we go, and when count reaches k, we output that cross and stop.
Thus we can avoid storing any large lists, we just need to iterate over all possible (x, i, j) in order and check condition.
What is the total number of (x,i,j) triplets we need to consider? For each possible radius x from 1 to maxX, we iterate i from x+1 to n-x, j from x+1 to m-x. The total number of checks = sum_{x=1}^{maxX} (n-2x)*(m-2x). For n=m=300, this sum is about 4.45 million, as computed. That's exactly the maximum number of crosses if all stars present, but we still need to check each potential cross. If many stars missing, we still have to iterate through all these positions? We could early break if there are no stars, but we need to check condition. However, we can compute the checks efficiently. 4.45 million is not huge; it's within a few million operations. For each check we need to verify 5 cells: center, up, down, left, right. That's 5 character comparisons. So total operations about 22 million, which is perfectly fine in Python.
But careful: For each x, we will loop over all i, j that could potentially be centers. However, some i,j may not even be a star, but we still check 5 cells. However, we can skip quickly if center is not a star, but we still do the check. To optimize, we could precompute a boolean grid of stars and maybe also prefix sums to quickly check if all four arm positions are stars. But given the low number of checks, simple direct checks are fine.
Thus the algorithm is:
-
Determine maxX = min(n, m) // something? Actually maxX = min(n-1, m-1) // 2? Because i must satisfy i-x >=1 => i >= x+1, i <= n-x. So x <= (n-1)//2 and x <= (m-1)//2. So maxX = min((n-1)//2, (m-1)//2). Actually integer division floor. But careful: if n=1, then maxX=0 because (1-1)//2=0, no crosses. So loop x from 1 to maxX inclusive.
-
Then for x from 1 to maxX:
for i from x+1 to n-x:
for j from x+1 to m-x:
if grid[i][j] == '' and grid[i-x][j] == '' and grid[i+x][j] == '' and grid[i][j-x] == '' and grid[i][j+x] == '*':
count += 1
if count == k:
output the coordinates in required order: central (i,j), up (i-x,j), down (i+x,j), left (i,j-x), right (i,j+x). Then exit.
-
If after loops count < k, output -1.
This is straightforward. Complexity: sum_{x=1}^{maxX} (n-2x)*(m-2x). For worst case n=m=300, that's about 4.45 million iterations. That's definitely fine.
But wait: In the examples, first input, they got output for k=1. Our enumeration would produce crosses in order, and first cross found would be at smallest radius? At radius 1, we go through rows from 2 to n-1, columns from 2 to m-1. The first cross would be the one with smallest row and then smallest col among radius 1 crosses. Let's test with first example map: We should verify that the first cross found by this enumeration is indeed the one in output. Output's central is (2,5). Let's see radius? The output coordinates: central (2,5), up (1,5), down (3,5), left (2,4), right (2,6). So radius =1. So smallest radius is 1. Among radius=1 crosses, central (2,5) is row=2, col=5. Could there be any radius=1 cross with row=2 and smaller column? Possibly (2,4)? Check if (2,4) is a star? In map: rows 1..5, columns 1..6:
Input:
1: ..... (columns:1 . 2 . 3 . 4 . 5 * 6 .)
2: ...** (columns:1 . 2 . 3 . 4 ? Actually string "...": positions:1 .,2 .,3 .,4 ,5 ,6 ? Let's index: col1:'.', col2:'.', col3:'.', col4:'', col5:'', col6:''? But length 6, so "..." means three dots then three stars, so col4='', col5='', col6=''.)
3: ..... (col1.,2.,3.,4.,5,6.)
4: ..... (col1.,2.,3,4.,5.,6.)
5: .**.. (col1.,2,3*,4*,5.,6.)
Given grid, check cross at center (2,4): center (2,4) is '', up (1,4) is '.'? row1 col4 from first line: col4 is '.'? Actually row1: ".....": col1.,2.,3.,4.,5*,6.. So (1,4) is '.'. So not a cross. (2,5): center (2,5) '', up (1,5) '', down (3,5) '', left (2,4) '', right (2,6) ''. So yes. So the first cross in enumeration for radius=1 with rows from 2 to 4, columns from 2 to 5? Actually for n=5,m=6, maxX? n=5 => maxX = min((5-1)//2=2, (6-1)//2=2) => 2. So x from 1 to 2. For x=1, i from 2 to 4, j from 2 to 5. The order: i=2: j=2,3,4,5. i=3: j=2,3,4,5. i=4: j=2,3,4,5. Check (2,2): center? row2 col2 is '.'? row2 col2 '.'? row2 "...**": col2 is '.' -> not star. (2,3): '.' -> no. (2,4): as we saw fails. (2,5): works. So the first valid cross is at (2,5). So count=1, and k=1 => output that. Good.
Second example: same map, k=2. The enumeration would count total crosses? Let's check: Are there other crosses? Possibly radius=1 cross at (3,4)? Let's test (3,4): row3 col4 from row3 ".....": col4 is '.'? Actually row3: ".....": col1.,2.,3.,4.,5*,6.. So (3,4) '.' so not. (3,? ) none. (4,?) maybe radius=1? (4,3): row4 ".....": col3 '', up (3,3) '.'? row3 col3 '.'; fails. (4,4): row4 col4 '.'; no. (4,5): row4 col5 '.'; no. (4,2): row4 col2 '.'; no. So only one radius=1 cross. For radius=2: i from 3 to 3? Actually for x=2, i from 3 to n-x=3? n=5, x=2 => i from 3 to 3, j from 3 to m-x=4? m=6 => j from 3 to 4. So check (3,3): center row3 col3 '.'; (3,4): center '.'; so none. So total crosses=1, thus output -1. Good.
Third example: 7x7 with pattern. Output is cross with central (4,4) and radius=3? Actually output coordinates: central (4,4), up (1,4), down (7,4), left (4,1), right (4,7). So radius =3. And they ask for k=2, meaning there is at least 2 crosses, and the second one is this. So there is a cross with smaller radius? Let's see the map:
7 7 2
......
.......
......
.**.*
......
.......
......
So rows:
1: ...... -> col4 star.
2: ....... -> all dots.
3: ...... -> col4 star.
4: ..* -> col1 star, col2 dot? Actually "..": characters:1 '',2 '.',3 '',4 '',5 '',6 '.',7 ''? Let's parse: length 7. ".**." -> index:1 '',2 '.',3 '',4 '',5 '',6 '.',7 ''. So stars at cols1,3,4,5,7.
5: ...... -> col4 star.
6: ....... -> all dots.
7: ...... -> col4 star.
So there is a central cross with radius 3 at (4,4) because up (1,4) star, down (7,4) star, left (4,1) star, right (4,7) star. That's radius 3. Also possibly radius 1 crosses? Check center (4,4) also radius 1? Up (3,4) star, down (5,4) star, left (4,3) star, right (4,5) star. So (4,4) also is a cross of radius 1! Actually up (3,4) is star (row3 col4 star), down (5,4) star, left (4,3) star, right (4,5) star. So (4,4) is a cross radius 1. Also radius 2? Check (4,4) radius 2: up (2,4) dot, so no. So there is at least cross radius 1 at (4,4). Also maybe other radius 1 crosses? (4,3)? center (4,3) star, up (3,3) dot? row3 col3 '.' so no. (4,5) star, up (3,5) '.'? row3 col5 '.'; no. (3,4) center? (3,4) star, up (2,4) dot; no. (5,4) center? up (4,4) star, down (6,4) dot; no. So only (4,4) for radius 1. So total crosses: radius 1 at (4,4) and radius 3 at (4,4) as well? Wait, same center can produce multiple radii if all arms at those distances have stars. In this map, for center (4,4): check radius 1: arms at distance 1 are all stars. radius 2: up (2,4) is dot, so fails. radius 3: up (1,4) star, down (7,4) star, left (4,1) star, right (4,7) star. So passes. So there are two crosses with the same center but different radii. So ordering by radius: radius 1 cross comes before radius 3 cross. Since radius 1 is smaller. So k=1 would be the radius 1 cross, k=2 would be the radius 3 cross. Their output shows the radius 3 cross for k=2, consistent.
Thus our enumeration order will produce radius 1 cross first (x=1, then iterate rows, columns). When x=1, we will check all centers that could have radius 1. Among them, we will find (4,4) maybe after checking some other possible centers? For n=7,m=7, x=1: i from 2 to 6, j from 2 to 6. The order: i=2: j=2..6; i=3: j=2..6; i=4: j=2..6; i=5: j=2..6; i=6: j=2..6. Let's see which of these centers are stars? From grid: stars positions: row1 col4, row3 col4, row4 col1,3,4,5,7, row5 col4, row7 col4. So centers must be stars themselves. So possible centers in the range i=2..6, j=2..6: Check each:
i=2: row2 all dots -> none.
i=3: row3 only col4 star; j=4 in range? j from 2 to 6 includes 4; (3,4) is star. Does it satisfy radius 1? Check up (2,4) dot, down (4,4) star, left (3,3) dot, right (3,5) dot. So fails.
i=4: row4 stars at cols 3,4,5? Actually row4 stars at cols1,3,4,5,7. In j=2..6, stars at j=3,4,5. Check each:
- (4,3): center star; up (3,3) dot -> fails.
- (4,4): center star; check up (3,4) star, down (5,4) star, left (4,3) star, right (4,5) star -> pass.
- (4,5): center star; up (3,5) dot -> fails.
i=5: row5 only col4 star; (5,4): center star; up (4,4) star, down (6,4) dot -> fails.
i=6: row6 all dots.
So only (4,4) passes for x=1. So the first cross (k=1) would be (4,4) radius 1. Then after finishing x=1 loop, we go to x=2, then x=3, etc. At x=3, i from 4 to 4? For n=7,m=7, x=3: i from 4 to 4, j from 4 to 4. So only (4,4) possible. Check: center star, up (1,4) star, down (7,4) star, left (4,1) star, right (4,7) star -> pass. That's second cross. So enumeration order gives correct ordering. So the simple triple loop works.
Thus we can implement exactly that.
Potential pitfalls:
- The map coordinates: rows 1..n, cols 1..m. Input lines are strings of length m. We'll store them in a list grid where grid[1] is first row? Use 0-indexed in code but we can offset. Simpler: Use 0-indexed: rows 0..n-1, cols 0..m-1. Then condition for cross: grid[i][j]=='', grid[i-x][j]=='', grid[i+x][j]=='', grid[i][j-x]=='', grid[i][j+x]=='*'. But careful: In the loop, x starts from 1. For i from x to n-1-x, since 0-indexed: i-x >=0 => i>=x; i+x <= n-1 => i <= n-1-x. So i range: for i in range(x, n-x): (since n-x exclusive? Actually n-x is the first index that i+x = n? If n=5, i max = 4? 0-index: i ranges from 0..4. For x=1, i must satisfy i-1>=0 => i>=1; i+1 <=4 => i<=3. So i in range(1, 4) i.e., 1,2,3? Actually range(1,4) gives 1,2,3. That's correct. In Python range(start, stop) stop exclusive, so we need i <= n-1-x, so stop = n-x? Because n-x gives the max i+1? Example n=5, n-x=4, range(x, n-x) yields range(1,4) -> 1,2,3. Works. Similarly j range from x to m-x. So loops: for i in range(x, n - x): for j in range(x, m - x):
But careful: The central star must be a star; we check that.
Now we need to output coordinates in 1-indexed system. So when we find a cross, we output (i+1, j+1) for central, up (i-x+1, j+1), down (i+x+1, j+1), left (i+1, j-x+1), right (i+1, j+x+1). Or we could store the positions.
Now we need to be careful about maxX: maxX = min(n, m) // 2? Actually in 0-index, condition i >= x and i <= n-1-x, so x <= floor((n-1)/2) and x <= floor((m-1)/2). So maxX = min( (n-1)//2, (m-1)//2 ). Because if n=1, (n-1)//2=0, no crosses. For n=2, (2-1)//2=0, no crosses. For n=3, (3-1)//2=1, so maxX=1 possible. Actually check: n=3, possible cross with x=1: i must be >=1 and <=1 => i=1 only. So yes x=1 works if m also >=3. So maxX computed correctly.
Now implement:
read n,m,k
grid = [list(input().strip()) for _ in range(n)] (or keep as strings, indexing grid[i][j])
maxX = min( (n-1)//2, (m-1)//2 )
count = 0
for x in range(1, maxX+1):
for i in range(x, n - x):
for j in range(x, m - x):
if grid[i][j] == '' and grid[i-x][j] == '' and grid[i+x][j] == '' and grid[i][j-x] == '' and grid[i][j+x] == '*':
count += 1
if count == k:
# output
print(i+1, j+1)
print(i-x+1, j+1)
print(i+x+1, j+1)
print(i+1, j-x+1)
print(i+1, j+x+1)
return (exit)
If after loops count < k: print(-1)
That's it.
But is this guaranteed to produce crosses in the correct order? Yes because we iterate x increasing, then i increasing, then j increasing. This matches ordering: radius smallest first, then row smallest first, then col smallest first. So the enumeration order is exactly the sorted order.
But we need to be careful: The ordering defined is: first by radius, then by central star's row (higher means smaller row number), then by column (leftmost). Since we iterate i from x to n-x (increasing i), row index increases (i larger means lower row). But "higher" means smaller row number. So we need ascending order of row number. Since we are iterating i from small to large, that matches "higher" first (small i => higher). Indeed row number = i+1, so smaller i => smaller row number => higher. So it's correct.
Now check for edge cases: Suppose two crosses with same radius but different rows: the one with smaller row (higher) should come first. That's satisfied.
Now confirm for same row, we iterate j increasing, so smaller column (left) first.
Thus the enumeration is exactly the sorted order.
But we must be cautious: Are there any crosses that share same radius but same center? No, each cross is defined by (center, radius). So each combination is unique.
Thus the algorithm is simple.
However, we need to ensure that for centers where radius is possible, we count each x separately. The triple loops cover all possible (x,i,j) where i and j are in the valid range for that x. So all potential crosses are examined.
Now complexity: For n=m=300, maxX=149, sum_{x=1}^{149} (300-2x)*(300-2x) = we computed ~4.45 million. That is okay.
But is it always exactly that sum? For n,m not equal, sum_{x} (n-2x)*(m-2x). For n=300,m=300, it's ~4.45M. For n=300,m=300, but many cells may not be stars, we still need to loop through all possible (i,j) for each x, which is that many. So worst-case time is about 4.5M iterations, each performing 5 character comparisons and an if and count increment. That's fine.
But wait: In the triple loops, we are scanning many cells that are not stars; but we still need to check grid[i][j] and the four arms. However, we can skip quickly if grid[i][j] is not star, we still need to check the if condition? Actually the if condition first checks grid[i][j] == '*', so if that fails, we skip the other checks. So only when center is star we check four more. So for maps sparse, overhead is low.
Thus total operations is manageable.
But consider n=300,m=300, maxX=149, loops: x from 1 to 149; for each, i from x to n-x-1 (since n-x exclusive? Actually range(x, n-x) gives i up to n-x-1, because stop is n-x. So number of i values per x = (n - x) - x = n - 2x. Similarly j count = m - 2x. So total iterations = sum_{x=1}^{maxX} (n-2x)*(m-2x). For n=m=300, this is sum_{x=1}^{149} (300-2x)^2. That sum we computed as 4,455,100. That's okay.
But note: maxX = min((n-1)//2, (m-1)//2). For n=300,m=300, (300-1)//2 = 149. So yes.
Thus algorithm is efficient.
But we must be careful about reading input: there may be spaces? Input lines contain exactly m characters, no spaces.
Now potential pitfalls: The index k up to 3e7, but total crosses may be up to 4.5M, so if k is larger, we output -1. But we need to count total crosses to know if k > total. We could count as we go, but we would only know after finishing loops. However, we can early exit if we find the k-th. But if k is greater than total, we need to output -1 after full enumeration. So we need to count total crosses. Could compute total while enumerating. But we might also precompute total number by scanning? However, we can just run the enumeration and keep count, and after loops, if count < k, print -1. But if k is large (e.g., 30 million) and total crosses is only up to 4.5 million, we will still iterate all 4.5 million checks and then output -1. That's acceptable because 4.5 million loops is fine.
But what if n and m are both 300, but the grid is all stars? That would produce 4.5 million crosses, but then the enumeration will iterate 4.5 million checks, and we might need to find k-th up to maybe 4.5 million. That's still okay. So overall, the triple nested loops are fine.
But wait: In the enumeration, we are counting crosses only when the condition holds. But is it possible that there are crosses with radius x > maxX? No, because maxX defined as the maximum possible radius given map dimensions. So we won't miss any.
Now confirm with the third example: n=7,m=7, maxX = min(6//2=3,6//2=3) = 3. So loops x=1,2,3. For x=2, i from 2 to 4 (since n-x=5? Wait 0-index: n=7, so n-1=6. x=2 => i from 2 to 4? Because range(x, n-x) = range(2,5) -> i=2,3,4. j from 2 to 4. That's 3x3=9 checks. Among these, maybe none are crosses. For x=3, i from 3 to 3? range(3,4) -> i=3 only; j from 3 to 3? range(3,4) -> j=3 only. So one check: (i=3,j=3) in 0-index corresponds to (4,4) 1-index. That's the cross. So total checks: x=1: i from 1 to 5? Actually n=7: range(1,6) -> i=1..5 (5 values); j from 1..5 (5 values) =>25 checks. x=2: range(2,5) -> i=2,3,4 (3 values); j=2,3,4 (3) =>9 checks. x=3: 1 check. Total =35 checks. Very small.
Thus algorithm is trivial.
But wait: Is the enumeration order exactly the ordering defined? Let's double-check ordering: "when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one." So sort by (radius, row ascending (higher = smaller row number), column ascending). Our enumeration: for radius from 1 to maxX, for i from x to n-x-1, j from x to m-x-1. i is row index (0-index) so smaller i = smaller row number = higher. So yes. And j increasing = leftmost first. So that's correct.
One nuance: The enumeration for a given radius x includes all possible centers (i,j) that could potentially host a cross of radius x (i.e., within bounds). However, we only count those that satisfy the star condition. The order among those is exactly as we iterate. However, note that for a given radius, the possible centers are only those with i between x and n-x-1 and j between x and m-x-1. This is the set of all possible centers that could have a cross of that radius (since the arms must be within bounds). So we iterate over them in row-major order. That's consistent.
Thus solution is simple.
Now test with first example:
n=5,m=6. maxX = min((5-1)//2=2, (6-1)//2=2) =2. So loops: x=1: i range(1,4) -> i=1,2,3? Wait n=5 0-index: rows 0..4. x=1: range(1,4) -> i=1,2,3. j range(1,5) -> j=1,2,3,4 (since m=6, m-x=5) -> j=1..4. That's 3*4=12 checks. We'll find cross at (i=1,j=4) (since row2 col5 1-index). count=1, k=1 output. Good.
Second example: same, k=2. After first cross count=1, continue scanning. After finishing x=1 loop, count=1. Then x=2: i range(2,3) -> i=2 only? n-x=3? range(2,3) -> i=2; j range(2,4) -> j=2,3? Actually m=6, m-x=4, range(2,4) -> j=2,3. So two checks: (2,2) and (2,3). Both fail. So count remains 1. After loops, count=1 <2, output -1. Good.
Third example: n=7,m=7, maxX=3. x=1: many checks, find (i=3,j=3) (since row4 col4 1-index) -> count=1, but k=2, so continue. x=2: no cross. x=3: find (i=3,j=3) again? Actually at x=3, i range(3,4) -> i=3; j range(3,4) -> j=3; check (3,3) and it's a cross. count becomes 2, so output. Good.
Thus correct.
Now we need to ensure that we don't miss crosses where the central star is not a star? That is impossible because the cross requires central star. So we check that first.
Edge Cases:
-
If n<3 or m<3, maxX = 0 because (n-1)//2 or (m-1)//2 yields 0. So no cross. Then if k=1 output -1.
-
If there are crosses but k larger than total, output -1.
-
Should we consider radius 0? No, because then the arms would coincide with central star, not 5 distinct stars. The definition says "any 5 stars so, that for some integer x (radius of the constellation) the following is true: the 2nd is ... x squares up". If x=0, then 2nd is same as 1st, but they are distinct stars. So x must be at least 1. So we start from 1.
-
The indexing of k starts at 1.
-
Output coordinates must be in separate lines, each with two integers. The order: central, up, down, left, right.
Now, let's test with a scenario where there are many crosses and we need k-th among many. Our enumeration will iterate all possible (x,i,j) even after we found k-th? We break early once count==k. So we don't need to generate all crosses if k is small. That's beneficial.
Potential issues: In some grids, there could be crosses with same center and different radii. The enumeration will count them in order of x, which is correct.
Now time complexity: In worst case where k is large (close to total) or we need to output -1, we may go through all loops. That's about 4.5 million loops for n=m=300, which is fine.
But wait: In loops, for each x, we are iterating i from x to n-x-1, and j from x to m-x-1. For x=1, this is (n-2)(m-2) = 298298 = 88804. That's a lot, but still fine. For x=2: 296*296 = 87616, etc. So total loops ~4.45 million. In Python, a triple nested loop with 4.5 million iterations is okay; but we also do 5 checks for each iteration when center is star. For all stars grid, the center is always star, so we will always do 5 checks. That's 22.5 million character comparisons. That's still okay (<0.2 seconds? Actually Python may be slower; but 4.5 million loops with some overhead might be around 0.3-0.5 seconds? Let's approximate: Python can do about 50 million simple operations per second? Not exactly; but on typical Codeforces machines, 10^7 operations might be borderline 1 second. 4.5 million loops with a few condition checks and array accesses should be well under 1 second. However, the nested loops also have overhead of Python's for loops; the innermost loop body is executed 4.5 million times, which is okay.
But we must be careful: In Python, accessing grid[i][j] for each iteration may be slightly slower if grid is list of strings; but we can convert each row to a list of characters or keep as string. Accessing a character in a string by index is O(1) and fairly fast. However, we need to do 5 accesses: grid[i][j], grid[i-x][j], grid[i+x][j], grid[i][j-x], grid[i][j+x]. That's fine.
Potential micro-optimizations: Precompute a boolean 2D array of ints (0/1) to speed up checks, but not necessary.
Alternatively, we could compute prefix sums to quickly check if a cell is star, but that's overkill.
Thus the solution is straightforward.
Now we must implement reading input and output.
Edge case: k may be up to 3e7, but if total crosses less than that, we output -1. Our algorithm will finish loops and count total, then output -1. That's fine.
One more nuance: Could there be crosses with radius larger than maxX? maxX defined as floor((n-1)/2) and floor((m-1)/2). Actually, if n=3, m=100, maxX = min(1,49)=1, so only radius 1 possible. That's correct because for radius 2, need i-2>=0 => i>=2, i+2<=2? n=3: i+2 <=2? i<=0 => impossible. So yes.
Thus algorithm is done.
But wait: We need to ensure that we interpret "x squares up" as moving towards row 1 (decreasing row index). Our coordinates: (row, column) with (1,1) top-left. So up means row decreases. That's consistent with our i-x (since i is row index 0-based, so i-x corresponds to row number i-x+1, which is smaller row number). So correct.
Now confirm with the output order: they output central, upper, lower, left, right. Our print statements match.
Potential issue: The ordering among crosses with same radius and same center? Not possible because each cross has distinct radius. But if two crosses have same radius? They are distinct centers. So fine.
Thus we can implement.
Now let's double-check sample 3 output: They output:
4 4
1 4
7 4
4 1
4 7
We would output central (i+1,j+1) = (4,4). up (i-x+1, j+1) = (4-3+1=2? Wait i=3 (0-index) for row 4, x=3 => up row = 3-3+1 = 1 -> (1,4). down = 3+3+1=7 -> (7,4). left = (4, 3-3+1=1) -> (4,1). right = (4, 3+3+1=7) -> (4,7). That's exactly the sample output. Note they output up first then down then left then right. The problem says: "Output the stars in the following order: central, upper, lower, left, right." So we print central, up, down, left, right. That matches.
Thus solution is correct.
Now I'll think about any hidden pitfalls: The map can have up to 300x300 = 90,000 stars. The triple loops iterate over all possible (x,i,j) that are within bounds. Could there be centers that are not stars but we still iterate over them? Yes, we still loop over them. That's fine.
Potential memory: storing grid as list of strings.
Now we need to output -1 if not enough constellations.
Implementation steps:
- Read n,m,k.
- Read grid lines into a list of strings (or list of list).
- Compute maxX = min((n-1)//2, (m-1)//2).
- count = 0
- For x in range(1, maxX+1):
for i in range(x, n - x):
for j in range(x, m - x):
if grid[i][j] == '' and grid[i-x][j] == '' and grid[i+x][j] == '' and grid[i][j-x] == '' and grid[i][j+x] == '*':
count += 1
if count == k:
output coordinates and exit.
- Print -1.
That's it.
But one more nuance: The loops for i and j as described: range(x, n - x) yields i from x to n-x-1 inclusive. That's correct because i <= n-1-x. Similarly for j. But we must confirm that n - x is correct when n=5, x=1: n - x = 4, range(1,4) gives 1,2,3. Indeed i=3 gives i+1=4? Actually i=3 is row 4? row indices: 0,1,2,3,4. i=3 corresponds to row 4, i+1=4, i-1=2 row2, i+1? Actually for radius 1, i must be at least 1 and at most 3? i=3: i+1=4 <=4? Yes, row index 4 is within 0..4. So i=3 is valid. But is i=3 allowed? For x=1, i=3: up row=2, down row=4. Row 4 exists (index 4). So yes. So the range is correct.
But what about i=4? i=4 would require down row=5 which is out of bounds (max index 4). So not included. So range(1,4) excludes 4. Good.
Now for j: similar.
Thus correct.
Now we need to ensure that we break out of loops after output. We can use a function and return, or set a flag.
Now, what about the case where there are no crosses? Then count stays 0, output -1.
Now, we need to think about potential performance improvements: The triple loops may be nested deep, but we can possibly break out early if we found the k-th cross. However, after finding it, we exit.
Now test with maximum input: n=300,m=300, all stars, k = total crosses (4,455,100). Our loops will go through all x,i,j, and we will count each cross. At the end, when count == k, we output the last cross. That will require processing all 4.5 million checks. That's okay.
But we need to ensure we don't have time limit issues. 4.5 million loops with 5 checks each is 22.5 million condition checks. In Python, that's maybe about 0.2-0.4 seconds? Actually typical Python can do about 50 million simple operations per second on modern CPUs? Might be less. Let's test mental: A for loop with 10 million iterations doing a simple operation might take around 0.5-1 second. So 4.5 million should be fine. But we have triple loops; the outer loops overhead also. Should be acceptable.
But we must consider that the innermost loop body includes indexing and boolean checks. Could be a bit slower but still likely under 1 second. However, Codeforces time limits are often 1-2 seconds. 4.5 million iterations might be okay, but we need to be careful about using Python's string indexing which is somewhat fast. However, note that for each x, we are iterating over i and j ranges that produce many iterations. For x=1, about 88k iterations; for x=2, about 87.6k; and so on. The total 4.45 million. That's fine.
But what if n=300,m=300, but not all stars? We still iterate the same number of (i,j) combos. So worst-case runtime is fixed by n,m, not by star density. That is good.
Thus this solution should pass.
Now let's double-check the calculation for maxX: For n=300,m=300, (n-1)//2 = 299//2 = 149. So maxX=149. That yields loops for x up to 149. For each x, i range length = n-2x = 300-2x. For x=1, 298; x=2,296; ... x=149, 300-298=2. So total iterations = sum_{x=1}^{149} (300-2x)(300-2x). That sum we computed as 4,455,100. Let's compute exactly to confirm: sum_{t=1}^{149} (300-2t)^2. Let s = 300-2t. When t=1, s=298; t=149, s=2. So it's sum_{s=2,4,6,...,298} s^2? Actually s decreases by 2 each step. So total = sum_{k=1}^{149} (2k)^2? Not exactly: For t=149, s=300-298=2. So s values: 298,296,...,2. That's even numbers from 2 to 298. So sum = sum_{e=2,4,...,298} e^2 = 4 * sum_{u=1}^{149} u^2? Because e=2u, e^2=4u^2, where u from 1 to 149. So sum = 4 * (149150299)/6. That product: 149150=22350, *299=6,682,650, /6=1,113,775, times 4 = 4,455,100. Yes.
So the number of total iterations is about 4.455 million.
Now, can there be any off-by-one error in the loops? For n=300, x=149, i range(149, n-x) => range(149, 151) because n-x = 300-149=151. So i values: 149,150? Wait range(149,151) yields 149 and 150. That's 2 values: i=149,150. But we expected i count = n-2x = 300-298=2. Yes, i=149 and 150. Check bounds: i=149: up row = 149-149=0 (valid), down row = 149+149=298 (<=299? 298 is <=299, valid). i=150: up row=1 (150-149=1), down row=299 (150+149=299). Both valid. So correct.
Now we need to ensure that we are not missing centers like (i=0?) Not possible because i must be >= x. So for x=149, i must be >=149, so i=149 is the smallest possible, which corresponds to row 150 in 1-index? Actually i=149 => row 150. So okay.
Thus loops are correct.
Now, I'm comfortable.
Let's test with a small custom case: n=3,m=3, all stars. So grid:
Then maxX = min((2)//2=1, (2)//2=1) =1. x=1: i range(1,2) => i=1 only (0-index) => row2; j range(1,2) => j=1 only => col2. Check center (1,1) is star; up (0,1) star; down (2,1) star; left (1,0) star; right (1,2) star. So count=1. So only cross. Our output for k=1 would be central (2,2), up (1,2), down (3,2), left (2,1), right (2,3). That's correct.
Now what about n=4,m=4 all stars? n=4,m=4 => maxX = min((3)//2=1, (3)//2=1)=1. x=1: i range(1,3) => i=1,2; j range(1,3) => j=1,2. So 4 potential centers. Check each: All stars, so all 4 are crosses? Let's verify for i=1,j=1 (row2 col2): up row1 col2 star, down row3 col2 star, left row2 col1 star, right row2 col3 star => yes. i=1,j=2 (row2 col3): up row1 col3 star, down row3 col3 star, left row2 col2 star, right row2 col4 star => yes. i=2,j=1 (row3 col2): up row2 col2 star, down row4 col2 star, left row3 col1 star, right row3 col3 star => yes. i=2,j=2 (row3 col3): up row2 col3 star, down row4 col3 star, left row3 col2 star, right row3 col4 star => yes. So 4 crosses. Ordering by radius 1, then row asc (i asc): i=1 before i=2. For i=1: j=1 then j=2; i=2: j=1 then j=2. So indices: 1: (2,2), 2: (2,3), 3: (3,2), 4: (3,3). That matches expected ordering. Good.
Now, what about crosses with radius >1 but same center? Not in this case.
Now, what about cross with radius=2 in a 5x5 all stars? n=5,m=5 => maxX = min((4)//2=2, (4)//2=2)=2. x=2: i range(2,3) => i=2 only; j range(2,3) => j=2 only. Check (i=2,j=2) (row3 col3) works? up row1 col3 star, down row5 col3 star, left row3 col1 star, right row3 col5 star. So yes. So there is also cross radius 2. In ordering, all radius 1 crosses (those with i=1..3, j=1..3? Actually for x=1: i=1,2,3; j=1,2,3 => 9 crosses) come before the radius 2 cross. So index of radius 2 cross is 10 if we have 9 radius 1 crosses. That matches enumeration: after finishing x=1 loop count=9, then x=2 loop count becomes 10. So correct.
Thus algorithm seems solid.
Now, consider that the input k can be up to 3e7 but total crosses may be less; we output -1.
Potential memory: storing grid as list of strings is fine.
One nuance: The problem statement says: "Your task is to find the constellation with index k by the given Berland's star map." And "Output -1 if number of constellations is less than k." So we just output -1 if count < k.
Now we should also consider the possibility of crosses with radius such that the arms are stars but the central star is not? That's not a cross, so ignore.
Now, I think we are done.
But before finalizing, let's double-check the ordering definition: "when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one." This matches (radius, row asc, col asc). So our enumeration order is correct.
Now, we need to ensure that we treat rows with smaller number as higher. So sorting by row asc. Good.
Now, consider if two crosses have same radius, same center? Not possible.
Now, we need to be careful about integer division when computing maxX. Use integer division //.
Now, I'll write the code.
Potential improvements: we might want to convert grid to a list of lists of bool to speed up checks. But string indexing is fine. However, note that for each iteration we do grid[i][j] etc; if grid is list of strings, accessing with negative index? Not possible because we ensure indices in range. So okay.
We can also precompute a 2D boolean array star[i][j] as True/False.
Let's do:
grid = [input().strip() for _ in range(n)]
Then in loops:
if grid[i][j] == '' and grid[i-x][j] == '' and grid[i+x][j] == '' and grid[i][j-x] == '' and grid[i][j+x] == '*':
Yes.
Now, test with sample 1: works.
Now, test with a case where n=300,m=300, all stars, k=4455100 (last). We should output the last cross. Which one is that? It would be the largest radius? Actually order: all radius 1 first, then radius 2, ..., radius 149. Within each radius, rows smallest to largest, columns smallest to largest. So the last cross would be with radius 149, and within radius 149, the centers are processed in row asc then col asc. For n=m=300, radius 149, possible centers are those i where i from 149 to 150 (0-index) and j from 149 to 150. So there are 4 centers: (149,149), (149,150), (150,149), (150,150). The order: i=149: j=149, then j=150; i=150: j=149, then j=150. So last cross is (i=150,j=150) which corresponds to row 151? Actually i=150 -> row 151, j=150 -> col 151? Wait i max = 150? 0-index rows: 0..299. So i=150 -> row 151? Actually row number = i+1, so row 151. Similarly col 151. But is that within bounds? For radius 149, the center must be at least row 150? Actually for radius 149, i must be >=149 and <=150? Since n=300, n-1=299, i <= 299-149=150. So i max =150. So row max = 151. Similarly column max =151. So the last cross is at (151,151). But does a cross with radius 149 exist with center at (151,151)? Up: row 2, down: row 300, left: col 2, right: col 300. All stars if full. So yes. So output would be that. So enumeration would find it as the last one when count reaches total. So if k equals total, we will output that cross.
Now, check if our loop order for x=149 yields centers: i from 149 to 150, j from 149 to 150. The order: i=149: j=149,150; i=150: j=149,150. So the last one is i=150,j=150. So that matches.
Now, we need to be careful that we break after printing, but if we found cross, we should exit program.
Implementation in Python:
import sys
def main():
import sys
data = sys.stdin.read().splitlines()
if not data:
return
first = data[0].split()
n, m, k = map(int, first)
grid = data[1:1+n]
maxX = min((n-1)//2, (m-1)//2)
count = 0
for x in range(1, maxX+1):
# precompute ranges maybe not necessary
for i in range(x, n - x):
row_i = grid[i]
row_i_up = grid[i-x]
row_i_down = grid[i+x]
# micro-optimization: we can store these rows for each i? but okay.
for j in range(x, m - x):
if row_i[j] == '' and row_i_up[j] == '' and row_i_down[j] == '' and row_i[j-x] == '' and row_i[j+x] == '*':
count += 1
if count == k:
# output
# coordinates: i+1, j+1 etc.
out = []
out.append(f"{i+1} {j+1}")
out.append(f"{i-x+1} {j+1}")
out.append(f"{i+x+1} {j+1}")
out.append(f"{i+1} {j-x+1}")
out.append(f"{i+1} {j+x+1}")
sys.stdout.write("\n".join(out))
return
print(-1)
if name == "main":
main()
This should work.
But we must ensure that we correctly handle the case where the grid lines may contain whitespace? The problem says each line has m characters, so no spaces.
Also, we should use sys.stdin.read().splitlines() for speed. But also we can use sys.stdin.readline for each line. Either is fine.
Now, note: In the loops, we use row_i = grid[i], etc. That's good to avoid repeated indexing of grid for each j. But we also need grid[i-x] and grid[i+x]. So we fetch those rows as well. However, for each i we could fetch them once. But inside inner loop, we also need row_i[j-x] and row_i[j+x]; row_i is a string, so indexing is fast. So fine.
Now, we need to ensure that we don't have any off-by-one errors in the ranges: In the inner loop, we used j in range(x, m - x). Because m-x is the exclusive stop. For m=6, x=1: range(1,5) -> j=1,2,3,4. That's correct as earlier. For j=4, j+x=5, which is valid index 5 (since m=6, indices 0..5). j-x=3, valid. Good.
Now test with sample 1 using code mentally.
Now, what about when n or m is less than 3? Then maxX = 0, loops not executed, count remains 0, so if k=1 output -1. That's correct because no cross can exist (needs at least 3 rows and 3 columns to have radius 1). For n=2,m=3, radius 1 requires i>=1 and i<=0? Actually n=2: n-1=1, (1)//2=0, so maxX=0. Indeed no cross.
Now, what about n=3,m=3? maxX=1, as we tested.
Now, all good.
Thus solution is ready.
But wait: The problem statement says: "the 2nd is on the same vertical line as the 1st, but x squares up". That implies that the central star is the 1st, then the 2nd is up, 3rd down, 4th left, 5th right. The order of output is central, upper, lower, left, right. That's exactly what we do.
Now, we should also note that k can be as large as 3e7, but total crosses maybe up to 4.5e6, so if k is greater, we output -1.
Now, confirm that we don't have any integer overflow issues: count is Python int, unlimited.
Now, time complexity is O( sum_{x} (n-2x)(m-2x) ) = O(nmmin(n,m))? Actually it's about O(nm*min(n,m)/2)? But it's bounded by about 4.5e6, so fine.
Thus final answer.
Potential improvement: Precompute a boolean matrix to speed up checks, but not needed.
Now, test with a custom case where there are many stars but we need to find cross with k large. Our loops will run many iterations. But that's fine.
Now, I'm going to write final answer.
But wait: I must verify that the ordering is indeed exactly as we iterate. Could there be any nuance that the radius x is defined as integer, and we assume x positive. Yes.
Now, edge: Two crosses with same radius and same row but different columns: we iterate j increasing, so leftmost first, correct.
Now, one more nuance: The problem's numbering starts from 1. So if k=1, we output the smallest cross.
Now, we might also need to consider that the grid reading: some lines may contain trailing spaces? Usually not. We'll strip each line.
Now, I think it's fine.
I'll now produce the final solution code.
But before finalizing, I'll check if there is any possibility that the triple loop might miss some crosses because we are not checking that the central star is distinct from the arm stars? They are automatically distinct because x>0 and positions differ. So no need to check.
Now, confirm that the coordinates for up, down, left, right are computed correctly relative to center.
Now, consider if the map contains characters other than '*' and '.'? According to problem, only these.
Now, I think we are done.
Now, I'll write the solution explanation.
But the question: In the analysis, I might want to mention that we can just iterate over all possible radii and centers in order, checking the condition.
Now, I'll produce final answer.
One more test: n=3,m=4, grid all stars. maxX = min((2)//2=1, (3)//2=1)=1. For x=1, i range(1,2) => i=1 only (0-index row 2). j range(1,3) => j=1,2? Because m=4, m-x=3, so range(1,3) => j=1,2. So two potential centers: (1,1) and (1,2). Check both: For (1,1): center row2 col2, up row1 col2 star, down row3 col2 star, left row2 col1 star, right row2 col3 star -> yes. For (1,2): center row2 col3, up row1 col3 star, down row3 col3 star, left row2 col2 star, right row2 col4 star -> yes. So crosses: (2,2) and (2,3). Order: (2,2) then (2,3). So k=1 first, k=2 second. Our loops produce that order: i=1, j=1 then j=2. Good.
Now, consider n=4,m=5 all stars. maxX = min((3)//2=1, (4)//2=2?) Actually (m-1)//2 = (4)//2=2, but (n-1)//2=1, so maxX=1. So only radius 1. i range(1,3) => i=1,2 (rows 2,3). j range(1,4) => j=1,2,3 (since m=5, m-x=4). So 2*3=6 potential centers. All should be crosses if all stars. Order: i=1: j=1,2,3; i=2: j=1,2,3. So rows: 2 then 3. So central stars: (2,2),(2,3),(2,4),(3,2),(3,3),(3,4). That matches ordering: smaller row first, then smaller column.
Now, everything is consistent.
Now, we can finalize.
Potential pitfalls: In Python, using range(x, n-x) for i, but if n-x <= x, the range is empty. For x = maxX, n - x >= x+? Actually for maxX, we have n - x >= x? Because maxX is floor((n-1)/2). For n odd: n=2t+1, maxX = t. Then n-x = 2t+1 - t = t+1, so range(t, t+1) yields only t. Good. For n even: n=2t, maxX = t-1 (since (n-1)//2 = (2t-1)//2 = t-1). Then n-x = 2t - (t-1) = t+1, range(t-1, t+1) yields t-1 and t? Let's compute: n=6, (5)//2=2, so maxX=2. For x=2, n-x=4, range(2,4) -> i=2,3. So i count = n-2x = 6-4=2. That's correct. So range works.
Now, ensure that for n even and x = maxX, n-x > x? For n=6, x=2, n-x=4, x=2, range(2,4) gives 2 values. So correct.
Thus loops cover exactly the required centers.
Now, one more check: For n=4,m=4, maxX=1. x=1: i range(1,3) -> i=1,2; j range(1,3) -> j=1,2. That's 4 centers as earlier.
Now, this is correct.
Now, final code.
But note: In the loops we used for i in range(x, n - x): This works for both odd and even n. Let's test with n=4,x=1: n-x=3, range(1,3) -> i=1,2 -> correct. For n=5,x=1: n-x=4, range(1,4) -> i=1,2,3 -> correct. For n=5,x=2: n-x=3, range(2,3) -> i=2 -> correct. For n=6,x=2: n-x=4, range(2,4) -> i=2,3 -> correct. For n=6,x=3? But maxX=2, so not used.
Thus fine.
Now, let's test with extreme n=300,m=300: x=149, n-x=151, range(149,151) -> i=149,150 -> correct.
Thus all good.
Now, I'll produce final answer. ```python
import sys
def main():
data = sys.stdin.read().splitlines()
if not data:
return
n, m, k = map(int, data[0].split())
grid = data[1:1+n] # each element is a string of length m
# maximum possible radius (x) given the field size
max_x = min((n-1)//2, (m-1)//2)
count = 0
for x in range(1, max_x+1):
# rows that can be centres for this radius
for i in range(x, n - x):
row_center = grid[i]
row_up = grid[i-x]
row_down = grid[i+x]
# columns that can be centres
for j in range(x, m - x):
if (row_center[j] == '*' and
row_up[j] == '*' and
row_down[j] == '*' and
row_center[j-x] == '*' and
row_center[j+x] == '*'):
count += 1
if count == k:
# output the five coordinates (convert to 1‑based)
out = [
f"{i+1} {j+1}",
f"{i-x+1} {j+1}",
f"{i+x+1} {j+1}",
f"{i+1} {j-x+1}",
f"{i+1} {j+x+1}"
]
sys.stdout.write("\n".join(out))
return
print(-1)
if name == "main":
main()