{"title":"题目描述","description":"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.\n\nTo 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.\n\nFor 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 answer instantly. But how long exactly?\n\n\n\nFigure L.1: Sample grid and the order in which test subjects explore the grid.\n\nIn 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.\n\nHaving 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?\n\n输入格式\nThe input describes a single grid. It starts with a line containing two integers \n\n\t\td\t\tx\n\t\n , \n\n\t\td\t\ty\n\t\n (\n\n1\n\n≤\n\n\t\td\n\t\n , \n\n\t\td\n\t\n ≤\n\n100\n\n1≤d \n\tx\n\t ,d \n\ty\n\t ≤100). The following \n\n\t\td\n\t\ny\n\t\n lines contain \n\n\t\td\n\t\nx\n\t\n characters each, and describe part of the test grid. The \n\n\t\n\tℎ\n\t\ni\n\t\n th\n\t\n character of the \n\n\t\n\tℎ\n\t\nj\n\t\n th\n\t\n line of this grid description specifies the contents of the grid cell at coordinate (\n\ni, \n\n\t\td\n\t\ny\n\t\n −\n\n\t\n+\n\n1\n\n\t\td\n\t\ny\n\t\n −j+1). The character is either '\.\' or '\X\', meaning that the cell is either empty, or contains a marker, respectively.\n\nThe total number of markers in the grid will be between \n\n1\n\n1 and \n\n100\n\n100, inclusive. All grid cells outside the range described by the input are empty.\n\n输出格式\nIn ICPC's experiment, a test subject knows they will start at some position (\n\nx, \n\ny) with \n\n1\n\n≤\n\nx≤\n\n\t\td\n\t\nx\n\n1≤x≤d \n\tx\n\t , \n\n1\n\n≤\n\ny≤\n\n\t\td\n\t\ny\n\n1≤y≤d \n\ty\n\t .\n\nOutput 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 \n\n1\n\n0\n\n−\n\n3\n\n10\n\n−3\n\n10 \n\n−3\n\n .\n\nThe second line should have the maximum number of steps necessary until one can identify the starting position.\n\nThe third line should list all starting coordinates (\n\nx, \n\ny) that require that maximum number of steps. The coordinates should be sorted by increasing \n\ny-coordinates, and then (if the \n\ny-coordinates are the same) by increasing \n\nx-coordinates.\nC++解答,回答中文内容:思路:\n1. 首先,我们需要遍历所有可能的起始位置。\n2. 对于每个起始位置,我们按照顺时针螺旋的方式探索网格,直到找到一个标记点。在探索过程中,我们记录经过的步数。\n3. 对于每个起始位置,我们重复步骤2多次,然后求平均步数,即为期望步数。\n4. 我们还需要记录最大步数和对应的起始位置。\n\n代码如下:\n\n#include \n#include \n#include \nusing namespace std;\n\nint main() {\n int dx, dy;\n cin >> dx >> dy;\n vector grid(dy);\n for (int i = 0; i < dy; i++) {\n cin >> grid[i];\n }\n\n int maxSteps = 0;\n vector<pair<int, int>> maxStartPos;\n int totalSteps = 0;\n int numTrials = 1000; // 进行1000次试验\n\n for (int trial = 0; trial < numTrials; trial++) {\n int steps = 0;\n for (int x = 0; x < dx; x++) {\n for (int y = 0; y < dy; y++) {\n int curX = x, curY = y;\n bool found = false;\n while (!found) {\n if (grid[curY][curX] == 'X') {\n found = true;\n } else {\n steps++;\n if (curX % 2 == 0) {\n if (curY == dy - 1 || grid[curY + 1][curX] != '.') {\n curX++;\n } else {\n curY++;\n }\n } else {\n if (curY == 0 || grid[curY - 1][curX] != '.') {\n curX++;\n } else {\n curY--;\n }\n }\n }\n }\n }\n }\n totalSteps += steps;\n\n if (steps > maxSteps) {\n maxSteps = steps;\n maxStartPos.clear();\n maxStartPos.push_back(make_pair(x, y));\n } else if (steps == maxSteps) {\n maxStartPos.push_back(make_pair(x, y));\n }\n }\n\n double expectedSteps = totalSteps / (double) numTrials;\n\n cout << expectedSteps << endl;\n cout << maxSteps << endl;\n sort(maxStartPos.begin(), maxStartPos.end());\n for (auto startPos : maxStartPos) {\n cout << startPos.first << " " << startPos.second << endl;\n }\n\n return 0;\n}\n

GPS vs. Maps: Finding Your Way in an Unbounded Grid - Solution with C++ Code