45th ICPC
ICPC 2021
Dhaka, Bangladesh · 12 problems
- 1Massachusetts Institute of Technology · MIT ZEROONEJerry Mao, Mingyang Deng, Xiao Mao11
- 2Peking University · inverted crossChaozhe Kong, Junyue Pan, Yuyang Zhou10
- 3The University of Tokyo · ___ KING ___Hirotaka Isa, Riku Kawasaki, Yuta Takaya9
Problems
12 problemsProblem A · Crystal Crosswind Time limit: 5 seconds
You are part of a scientific team developing a new technique to image crystal structures at the molecular level. The technique involves blowing a very fine wind over the surface of the crystal at various angles to detect boundaries (indicated by molecules that are exposed to the wind). This is repeated with different wind directions and the boundaries observed for each direction are recorded. Your team has already collected the data, but – as is often the case with applied science – now the real work, analysis, must begin.
For a given crystal, you will receive the directions in which wind blew over the surface, and the locations of all boundaries encountered by each of these winds. For a wind blowing in direction , a boundary is defined as a location such that a molecule exists at and no molecule exists at . Note that for technical reasons and are not necessarily relatively prime.
The data might not uniquely determine the structure of the crystal. You must find the two unique struc- tures with the minimal and maximal number of molecules consistent with the observations.
For example, in the first sample input, nine different molecules are directly encountered by the given winds. There must be a molecule at location because otherwise would be a boundary for the third wind. For similar reasons, there must be molecules at and . There cannot be any further molecules as they would result in additional observations for some of the winds.
Input
The first line of input contains three integers , , and , where and () are the maximum dimensions of the crystal structure, and () is the number of times wind was blown over the crystal.
Each of the remaining lines specifies the data for one wind. These lines each start with two integers and ( and , but not both zero) denoting the direction of the wind. Then comes an integer () giving the number of boundaries encountered by this wind. The line finishes with distinct pairs of integers ( and ) listing each observed boundary.
You may assume the input is consistent with at least one crystal and that no molecules exist outside the specified dimensions.
Output
Output two textual representations of the crystal structure separated by an empty line. Each structure has rows of characters, with the top-left corner corresponding to location . The first is the structure with the minimal number of molecules consistent with the observations, the second is the maximal one. Use ‘#’ for a location where a molecule exists and ‘.’ for a location where no molecule exists.
ICPC World Finals 2021 Problem A: Crystal Crosswind
Sample Input 1
6 6 3 1 1 3 3 1 1 3 2 2 0 2 6 3 1 1 3 2 2 6 4 5 3 4 2 1 -1 4 1 3 2 4 3 5 4 6Sample Output 1
..#... .#.#.. #.#.#. .#.#.# ..#.#. ...#.. ..#... .#.#.. #.#.#. .#.#.# ..#.#. ...#..Sample Input 2
5 4 2 1 0 6 1 1 4 1 2 2 5 2 2 3 3 4 0 -1 7 1 1 4 1 5 2 2 3 3 4 4 4 5 4Sample Output 2
#..#. .#..# .#... ..### ##.## .##.# .###. ..###ICPC World Finals 2021 Problem A: Crystal Crosswind
Problem B · Dungeon Crawler Time limit: 5 seconds
The helix key Image generated by DALL-E Alice and Bob are in charge of testing a new escape room! In this escape room, customers are trapped in a dungeon and have to explore the entire area. The dungeon consists of rooms connected by exactly corridors. It is possible to travel between any pair of rooms using these corridors.
Two of the dungeon rooms are special. One of these rooms contains a pro- tective idol known as the “helix key.” A different room contains a nasty “dome trap,” which prevents the player from moving once activated. Enter- ing the room with the trap before acquiring the key will result in the player being trapped in the dungeon forever. The player cannot start in the same room as the key or the trap.
There are different scenarios that Alice and Bob wish to examine. In the th scenario, the player starts in room , the key is in room , and the trap is in room . For each scenario, compute the minimum amount of time needed to explore the entire dungeon without getting trapped.
Input
The first line of input contains two integers and , where () is the number of rooms and () is the number of scenarios to consider. Rooms are numbered from to . The next lines each contain three integers , , and indicating that there is a corridor between rooms and () that takes time () to traverse.
Then follow lines: the th of these lines contains three distinct integers , , and ( ) indicating the room where the player starts, the room with the key, and the room with the trap, respectively.
Output
For each scenario, output the minimum amount of time needed to visit every room at least once. If it is impossible to visit every room at least once, output impossible.
Sample Input 1
5 4 1 2 3 1 3 1 3 4 4 3 5 2 1 2 4 1 4 2 5 2 1 4 3 1Sample Output 1
15 17 impossible 12ICPC World Finals 2021 Problem B: Dungeon Crawler
Sample Input 2
7 4 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 2 3 5 4 1 3 1 4 2 4 5Sample Output 2
11 impossible 10 10ICPC World Finals 2021 Problem B: Dungeon Crawler
Problem C · Fair Division Time limit: 3 seconds
Image by ZedH at Pixabay After sailing the Seven Seas and raiding many ships, Cap’n Red and his crew of fellow pirates are finally ready to divide their loot. According to ancient traditions, the crew stands in a circle ordered by a strict pirate hierarchy. Cap’n Red starts by taking a fraction of the loot and passing the remainder on to the next pirate. That pirate takes the same fraction of the loot left over by the previous pirate and passes the remainder on to the following pirate. Each pirate behaves in the same way, taking a fraction of what is left and passing on the rest. The last pirate in the hierarchy passes the remainder on to Cap’n Red, who starts another round of this “fair” division, and so on, indefinitely.
Fortunately, pirates in the 21st century can use a computer to avoid this lengthy process and constant nitpicking when the fraction does not ex- actly divide the loot at some step. You have been captured by the pirates and asked to come up with a suitable fraction . As an incentive, Cap’n Red has promised to leave you alive if you succeed.
The fraction needs to be a rational number strictly between and . It is not necessary that exactly divides the loot remaining at any step of the round-robin process described above. However, the total loot that would be assigned to each pirate by carrying out this process infinitely needs to be an integer.
Input
The input contains one line with two integers and , where () is the number of pirates including Cap’n Red and () is the total value of their loot.
Output
Output one line with two positive integers and , where as specified above. If there are multiple suitable fractions, choose one with the smallest . Among multiple suitable fractions with the same smallest choose the one with the smallest . If there is no suitable fraction, output impossible instead and hope for mercy.
Sample Input 1
8 51000Sample Output 1
1 2Sample Input 2
6 91000Sample Output 2
2 3Sample Input 3
10 1000000000000000000Sample Output 3
impossibleICPC World Finals 2021 Problem C: Fair Division
Problem D · Guardians of the Gallery Time limit: 5 seconds
Your local art gallery is about to host an exciting new exhibition of sculptures by world-renowned artists, and the gallery expects to attract thousands of visitors. Unfortunately, the exhibition might also attract the wrong kind of visitors, namely burglars who intend to steal the works of art. In the past, the gallery directors did not worry much about this problem, since their permanent collection is, to be honest, not really worth stealing.
The gallery consists of rooms, and each sculpture in the new exhibition will be placed in a different room. Each room has a security guard and an alarm to monitor the artwork. When an alarm sounds, the guard will run (without leaving the room) from their post to a position where they can see the sculpture directly. This is to check whether the sculpture has in fact been stolen, or whether this is yet another false alarm.
To figure out where to best station the security guard, the gallery directors would like to know how long it takes for the guard to see a given sculpture. They hope that you can help!
Every room is on a single floor, and the layout of the walls can be approximated by a simple polygon. The locations of the guard and the sculpture are distinct points strictly inside the polygon. The sculpture is circular, with a negligibly small (but positive) radius. To verify that the sculpture is still present, the guard needs to be able to see at least half of it.
Figure D.1 illustrates two examples. In each case, the guard starts at the blue square on the left, and the sculpture is located at the red circle on the right. The dotted blue line shows the optimal path for the guard to move. Once the guard reaches the location marked by the green diamond, half of the sculpture

Figure D.1: Illustration of sample inputs.
Input
The first line of input contains an integer (), the number of vertices that describe the polygon. This is followed by lines each containing two integers and (), giving the coordinates of the polygon vertices in counterclockwise order. The next line contains two integers and , which specify the location of the guard. Finally, the last line contains two integers and , which specify the location of the center of the sculpture. The polygon is simple, that is, its vertices are distinct and no two edges of the polygon intersect or touch, other than consecutive edges which touch at their common vertex. In addition, no two consecutive edges are collinear.
ICPC World Finals 2021 Problem D: Guardians of the Gallery
Output
Output the minimum distance that the guard has to move to be able to see at least half the sculpture. Your answer must have an absolute or relative error of at most .
Sample Input 1
8 0 0 20 0 20 30 60 30 60 0 80 0 80 50 0 50 10 10 70 10Sample Output 1
58.137767414994535Sample Input 2
11 0 0 4 0 4 1 5 1 5 0 7 0 7 2 3 2 3 1 2 2 0 2 1 1 6 1Sample Output 2
2.0ICPC World Finals 2021 Problem D: Guardians of the Gallery
Problem E · Hand of the Free Marked Time limit: 2 seconds
Example placement of cards for There is a fairly well-known mentalism trick known as the Fitch Cheney trick. From a deck of playing cards, are selected uniformly at random and given to an assistant while the magician is out of the room. The assistant places of the selected cards on a table, face up, and the single re- maining card face down. The cards are placed in a single row with the face-down card at the end (see the picture for an example). The magician enters the room, looks at the cards on the table, and announces what the th card is, although its face is hidden. The trick is typically done with and .
The assistant uses two ways of passing information to the magician. First, they can pick which one of the cards to keep hidden. Second, they can rearrange the other cards in a specific way. For the case and both techniques are needed, since there are only ways of rearranging four cards, which is not enough to reliably signal the fifth card. It is an interesting exercise to come up with a simple, easy-to-remember strategy for executing this trick, but right now you have another concern.
You were planning to perform this trick today, but just now you have learned that the deck has more cards than you expected. The trick may be impossible! In desperation, you have decided to cheat a little. You have distinguishable ways of marking the backs of the playing cards. You have marked the backs of all cards, allowing you to narrow down the possibilities for the th card. For example, if there are cards marked with a particular method, and you see that the back of the th card is marked with that method, you know it must be one of those cards.
Determine the probability that you will successfully guess the th card, assuming you and the assistant execute an optimal (but likely very complicated!) strategy.
Input
The input contains one line with several integers. The first integer gives (), the number of cards that will be selected. The second integer gives (), the number of ways of marking the cards. The line is completed by positive integers, giving the number of cards marked with each distinct method. The sum of these integers is (), which is the size of the deck.
Output
Output the highest possible probability of guessing the th card correctly, accurate up to an absolute error of .
Sample Input 1
4 1 28Sample Output 1
0.96Sample Input 2
3 3 5 12 3Sample Output 2
0.854385964912ICPC World Finals 2021 Problem E: Hand of the Free Marked
Problem F · Islands from the Sky Time limit: 2 seconds
You might never have heard of the island group of Iceepeecee, but that suits their inhabitants just fine. Located in a remote part of the South Pacific, they are truly off the beaten track, without any regular air or sea traffic, and they have remained a tropical paradise with unspoiled local fauna and flora.
Being off the map is great when you don’t want to be overrun by hordes of tourists, but not so ideal when you actually do need a map for some reason. One such reason came up recently: Iceepeecee’s central government needs an exact map of the islands to apportion government funds. Even tropical paradises need money, so Iceepeecee needs a map!
The easiest way to create a map would be an aerial survey. After dismissing chartering planes as too expensive, building an air balloon as too dangerous, and fitting carrier pigeons with cameras as too cruel to animals, they had a brilliant idea. Even with its remote location, there are still plenty of commercial airplanes crossing the skies above Iceepeecee. What if one mounted cameras on flights that are already scheduled to fly anyway? That would be a cheap solution to the problem!
Iceepeecee’s plan is to install line-scan cameras on the planes. These cameras point straight downwards and collect images one line segment at a time, orthogonal to the flight path. The photographed line segment will be determined by the altitude that the plane is flying at, and the camera’s aperture angle (see Figure F.1). Greater angles mean that the camera can see more, but also that the camera is more expensive.
Moreover, Iceepeecee wants to make sure that each island is observed in its entirety by at least one flight. That means it is not sufficient that an island is only partially photographed by multiple flights, even if the combination of the photographs covers the whole island.
Flight paths follow straight line segments in three-dimensional space, that is, (see Figure F.2), where the -coordinates give the altitude of the plane. Photographs are taken only along these line segments.
Given the location of their islands and flights, Iceepeecee wants to find the smallest aperture angle that

Figure F.2: Surveying three islands via two flight paths. This corresponds to the first sample input.
ICPC World Finals 2021 Problem F: Islands from the Sky
Input
The input describes a set of islands and flight paths. It starts with a line containing two integers and , the number of islands, and the number of flight paths, respectively (). This is followed by descriptions of the islands. Each island description starts with a line containing a single integer , the number of vertices of the polygon describing the th island (). It is followed by lines, each containing two integers , , specifying the vertices for the th island in counterclockwise order. Each island’s polygon is simple, that is, its vertices are distinct and no two edges of the polygon intersect or touch, other than consecutive edges which touch at their common vertex. Different islands do not intersect or touch.
The input concludes with another lines, each describing a flight path. Each such line contains six integers , , , , , (, and ). They specify that a flight takes place from to .
Output
Output the smallest angle (in degrees) that allows for a complete survey of the islands with the given flights. The answer should be exact to an absolute or relative error of . If there is no such angle, then output impossible. The input is chosen such that if the coordinates of the island vertices are changed by at most , then the answer will not change more than the allowed rounding error.
Sample Input 1
3 2 3 20 30 50 50 10 50 4 40 20 60 10 75 20 60 30 4 45 60 55 55 60 60 55 65 0 30 20 78 70 5 55 0 20 70 60 10Sample Output 1
48.031693036Sample Input 2
1 1 4 0 0 10 0 10 10 0 10 5 5 10 15 5 10Sample Output 2
impossibleICPC World Finals 2021 Problem F: Islands from the Sky
Problem G · Mosaic Browsing Time limit: 6 seconds
The International Center for the Preservation of Ceramics (ICPC) is searching for motifs in some ancient mosaics. According to the ICPC’s definition, a mosaic is a rectangular grid where each grid square contains a colored tile. A motif is similar to a mosaic but some of the grid squares can be empty. Figure G.1 shows an example motif and mosaic.
The rows of an mosaic are numbered to from top to bottom, and the columns are numbered to from left to right.
A contiguous rectangular subgrid of the mosaic matches the motif if every tile of the motif matches the color of the corresponding tile of the subgrid. Formally, an motif appears in an mosaic at position if for all , , the tile exists in the mosaic and either the square in the motif is empty or the tile at in the motif has the same color as the tile at in the mosaic.
Given the full motif and mosaic, find all occurrences of the motif in the mosaic.

Figure G.1: Motif (left) and mosaic (right) of Sample Input 1.
Input
The first line of input contains two integers and , where and () are the number of rows and columns in the motif. Then lines follow, each with integers in the range , denoting the color of the motif at that position. A value of denotes an empty square.
The next line of input contains two integers and where and () are the number of rows and columns in the mosaic. Then lines follow, each with integers in the range , denoting the color of the mosaic at that position.
ICPC World Finals 2021 Problem G: Mosaic Browsing
Output
On the first line, output , the total number of matches. Then output lines, each of the form where is the row and is the column of the top left tile of the match. Sort matches by increasing , breaking ties by increasing .
Sample Input 1
2 2 1 0 0 1 3 4 1 2 1 2 2 1 1 1 2 2 1 3Sample Output 1
3 1 1 1 3 2 2ICPC World Finals 2021 Problem G: Mosaic Browsing
Problem H · Prehistoric Programs Time limit: 6 seconds
Clay tablet with undeciphered script Source: Wikimedia Commons Archaeologists have discovered exciting clay tablets in deep layers of Alutila Cave. Nobody was able to decipher the script on the tablets, ex- cept for two symbols that seem to describe nested structures not unlike opening and closing parentheses in LISP. Could it be that humans wrote programs thousands of years ago?
Taken together, the tablets appear to describe a great piece of work – perhaps a program, or an epic, or even tax records! Unsurprisingly, af- ter such a long time, the tablets are in a state of disorder. Your job is to arrange them into a sequence so that the resulting work has a prop- erly nested parenthesis structure. Considering only opening and closing parentheses, a properly nested structure is either
-
(), or
-
(), where is a properly nested structure, or
-
, where and are properly nested structures.
Input
The first line of input contains one integer (), the number of tablets. Each of the remaining lines describes a tablet, and contains a non-empty string of opening and closing parentheses; symbols unrelated to the nesting structure are omitted. The strings are numbered from to in the order that they appear in the input. The input contains at most parentheses.
Output
Output a permutation of the numbers from to such that concatenating the strings in this order re- sults in a properly nested structure. If this happens for multiple permutations, any one of them will be accepted. If there is no such permutation, output impossible instead.
Sample Input 1
2 ())())() ((()Sample Output 1
2 1ICPC World Finals 2021 Problem H: Prehistoric Programs
Sample Input 2
5 ( )) (( )) (Sample Output 2
1 5 3 2 4Sample Input 3
2 (( )Sample Output 3
impossibleICPC World Finals 2021 Problem H: Prehistoric Programs
-
Problem I · Spider Walk Time limit: 6 seconds
Image by FBR Charlotte the spider sits at the center of her spiderweb, which consists of a series of silken straight strands that go from the center to the outer boundary of the web. Charlotte’s web also has bridges, each of which connects two adjacent strands. The two endpoints of a bridge always have the same distance to the center of the spiderweb.
When Charlotte has finished a late-night feasting in the cen- ter and wants to retreat to some corner, she walks to the edge on autopilot. To do this, she picks a starting strand, and walks along it until she meets the first bridge on that strand. She will cross the bridge and go to the other strand, and then keeps walking outwards until she meets another bridge. Then she will cross that bridge, and repeat this process, until there are no more bridges on the current strand, and then she will walk to the end of the current strand. Note that Charlotte must cross all the bridges that she meets. Figure I.1 illustrates one path Charlotte could take.
Charlotte’s favorite corner to sleep in during the daytime is at the end of strand . For each possible starting strand, she wants to know the minimum number of bridges to add to the original web in order to end at . Charlotte can add a bridge at any point along the strand, as long as the added bridge does not touch any other bridge. The two endpoints of any added bridge must have the same distance to the center of the spiderweb, and the bridge must connect two adjacent strands.

Figure I.1: The path starting from strand on the spiderweb in Sample Input 1.
ICPC World Finals 2021 Problem I: Spider Walk
Input
The first line of input has three integers , , and , where () is the number of strands, () is the number of bridges, and () is Charlotte’s favorite strand. Strands are labeled from to in counterclockwise order. Each of the remaining lines contains two integers and describing a bridge, where () is the bridge’s distance from the center of the spiderweb and () is the first strand of the bridge in counterclockwise order. Specifically, if , then the bridge connects strands and . If , then the bridge connects strands and . All bridge distances are distinct.
Output
Output lines, where the th line is the minimum number of bridges Charlotte needs to add in order to end at strand after walking on autopilot from strand .
Sample Input 1
7 5 6 2 1 4 3 6 3 8 7 10 5Sample Output 1
2 1 1 1 0 1 2Sample Input 2
4 4 2 1 1 2 2 3 3 4 4Sample Output 2
1 1 0 1ICPC World Finals 2021 Problem I: Spider Walk
Problem J · Splitstream Time limit: 3 seconds
Ganga-Brahmaputra delta (ESA, CC BY-SA 3.0 IGO) A splitstream system is an acyclic network of nodes that processes finite sequences of numbers. There are two types of nodes (illustrated in Figure J.1):
-
A split node takes a sequence of numbers as input and dis- tributes them alternatingly to its two outputs. The first number goes to output , the second to output , the third to output , the fourth to output , and so on, in this order.
-
A merge node takes two sequences of numbers as input and merges them alternatingly to form its single output. The output contains the first number from input , then the first from input , then the second from input , then the second from input , and so on. If one of the input sequences is shorter than the other, then the remaining numbers from the longer sequence are simply transmitted without being merged after the shorter

Figure J.1: Illustration of how split and merge nodes work.
The overall network has one input, which is the sequence of positive integers . Any output of any node can be queried. A query will seek to identify the th number in the sequence of numbers for a given output and a given . Your task is to implement such queries efficiently.
Input
The first line of input contains three integers , , and , where () is the length of the input sequence, () is the number of nodes, and () is the number of queries.
The next lines describe the network, one node per line. A split node has the format S , where , and identify its input, first output and second output, respectively. A merge node has the format M , where , and identify its first input, second input and output, respectively. Identifiers , and are distinct positive integers. The overall input is identified by , and the remaining input/output identifiers form a consecutive sequence beginning at . Every input identifier except appears as exactly one output. Every output identifier appears as the input of at most one node.
ICPC World Finals 2021 Problem J: Splitstream Each of the next lines describes a query. Each query consists of two integers and , where () is a valid output identifier and () is the index of the desired number in that sequence. Indexing in a sequence starts with .
Output
For each query and output one line with the th number in the output sequence identified by , or none if there is no element with that index number.
Sample Input 1
200 2 2 S 1 2 3 M 3 2 4 4 99 4 100Sample Output 1
100 99Sample Input 2
100 3 6 S 1 4 2 S 2 3 5 M 3 4 6 6 48 6 49 6 50 6 51 6 52 5 25Sample Output 2
47 98 49 51 53 100Sample Input 3
2 3 3 S 1 2 3 S 3 4 5 M 5 2 6 3 1 5 1 6 2Sample Output 3
2 none noneICPC World Finals 2021 Problem J: Splitstream
-
Problem K · Take On Meme Time limit: 4 seconds
The Internet can be so fickle. You work for a small ad agency, Mimi’s Mammoth Memes. Your ad campaigns are very cheap, and rely on the hope of producing the next hit viral meme. Unfortunately, the last four hundred or so memes have failed to take off, despite having been precisely engineered to appeal to every single person on Earth. You’re not sure what exactly went wrong, but you’ve decided to try a new approach: crowd sourcing!
According to your scientific meme theory, all memes can be rated from to on two scales: xan- thochromism, and yellowishness, also known as values. Obviously (you think), the best memes are memorable for being particularly xanthochromic, yellowish, unxanthochromic, or unyellowish. You feel that the “quality” of any meme is directly measurable as its squared Euclidean distance () from the Base Meme , otherwise known as All Your Base.
To produce the ultimate viral meme, you’ll be taking your company’s last few failed memes and throwing them into a tournament, decided by online voting. The tournament can be represented as a rooted tree. Input memes come in at the leaves, and at each internal node, a vote will be held among its child memes . After the vote, all the memes will be horrifically mangled and merged into a brand new meme, specifically calculated to emphasize the winner and de-emphasize all the losers: the resultant value will be
where is if the th child won, and otherwise. The value is computed similarly. This new meme will move on to the next vote in the tournament – or, if there is no parent, it will be declared the champion and the ultimate meme!
You already have the structure of the tournament planned out, including all the input memes and the internal voting nodes. What is the largest possible quality for any meme that the tournament could produce?
Input
The first line of input contains an integer (), giving the total number of nodes in the tournament tree. The next lines each describe a single tree node indexed from to . The line for node starts with an integer (), the number of children of that node. If is , then node is an input meme and there will be two more integers and () describing it. If , then different integers () will follow, giving the indices of the nodes entering this voting step.
All input memes will eventually be merged into the final output meme at node . The complete tree will have a height of no more than .
Output
Output the largest possible quality for the champion meme at node .
ICPC World Finals 2021 Problem K: Take On Meme
Sample Input 1
4 3 2 3 4 0 10 1 0 3 6 0 2 7Sample Output 1
169Sample Input 2
8 3 4 2 5 2 3 8 0 -3 9 0 -5 -7 2 6 7 0 1 4 0 -3 -1 0 1 4Sample Output 2
314ICPC World Finals 2021 Problem K: Take On Meme
Problem L · Where Am I? Time limit: 2 seconds
Who am I? What am I? Why am I? These are all difficult questions that have kept philosophers reliably busy over the past millennia. But when it comes to “Where am I?”, then, well, modern smartphones and GPS satellites have pretty much taken the excitement out of that question.
To add insult to the injury of newly unemployed spatial philosophers formerly pondering the “where” question, the Instant Cartographic Positioning Company (ICPC) has decided to run a demonstration of just how much more powerful a GPS is compared to old-fashioned maps. Their argument is that maps are useful only if you already know where you are, but much less so if you start at an unknown location.
For this demonstration, the ICPC has created a test area that is arranged as an unbounded Cartesian grid. Most grid cells are empty, but a finite number of cells have a marker at their center (see Figure L.1(a) for an example with five marked cells). All empty grid cells look the same, and all cells with markers look the same. Suppose you are given a map of the test area (that is, the positions of all the markers), and you are placed at an (unknown to you) grid cell. How long will it take you to find out where you actually are? ICPC’s answer is clear: potentially a very, very long time, while a GPS would give you the

Figure L.1: Sample grid and the order in which test subjects explore the grid.
In the trial, test subjects will explore their environment by following an expanding clockwise spiral whose first few steps are shown in Figure L.1(b). The starting cell is labeled “0”, and the numbers show the order in which other cells are visited. The test subjects can see a marker only if they are at its grid cell, and they will stop their exploration as soon as they know where they are based on the grid cells that they have seen so far. That means that the observed sequence of empty and marked grid cells could have begun only at a single possible starting position. The grid is unbounded, but the exploration will be finite since once a test subject has seen all markers on the grid, they’ll definitely know where they are.
Having hundreds of test subjects literally running in circles can be expensive, so the ICPC figures that writing a simulation will be cheaper and faster. Maybe you can help?
ICPC World Finals 2021 Problem L: Where Am I?
Input
The input describes a single grid. It starts with a line containing two integers (). The following lines contain characters each, and describe part of the test grid. The th character of the th line of this grid description specifies the contents of the grid cell at coordinate . The character is either ‘.’ or ‘X’, meaning that the cell is either empty, or contains a marker, respectively.
The total number of markers in the grid will be between 1 and 100, inclusive. All grid cells outside the range described by the input are empty.
Output
In ICPC’s experiment, a test subject knows they will start at some position with , .
Output three lines. The first line should have the expected number of steps needed to identify the starting position, assuming that the starting position is chosen uniformly at random. This number needs to be exact to within an absolute error of .
The second line should have the maximum number of steps necessary until one can identify the starting position.
The third line should list all starting coordinates that require that maximum number of steps. The coordinates should be sorted by increasing -coordinates, and then (if the -coordinates are the same) by increasing -coordinates.
Sample Input 1
5 5 ....X .X... ..... X..X. ..X..Sample Output 1
9.960 18 (1,4) (4,5)Sample Input 2
5 1 ..XX.Sample Output 2
4.600 7 (1,1) (5,1)ICPC World Finals 2021 Problem L: Where Am I?