#P6738. [BalticOI 2014] Cop and Robber (Day1)
[BalticOI 2014] Cop and Robber (Day1)
Description
A cop chases a robber. The cop will choose a street corner to guard. In each turn, the cop may choose to stay in place or move to an adjacent corner. The robber also starts at a street corner. Since he knows the map, he will choose the best strategy to move, but the robber is not allowed to stay in place.
In each round, the cop moves first, then the robber moves. There are two possible outcomes:
- The robber can avoid the cop forever.
- At some street corner, the robber and the cop meet, and the robber is arrested.
Determine whether the cop can catch the robber. If yes, output the minimum number of rounds.
Your code must assume that the robber plays with a winning strategy.
Interactive protocol
You need to implement two functions to interact with the interactor. Their prototypes are:
int start(int N, bool A[MAX_N][MAX_N]);
int nextMove(int R);
-
start(N, A)passes in the following parameters:-
— the number of street corners (corners are numbered from to ).
-
— a 2D array describing the alleys: for all , if and are not connected, then , otherwise .
All alleys are bidirectional (i.e. for all and , ), and there are no self-loops (i.e. for all , ). In addition, you may assume that the cop can always reach any street corner from any other street corner through the alleys.
-
If the cop can catch the robber on the map described by the parameters, the function start should return the index of the street corner where the cop and robber will meet. Otherwise, return .
nextMove(R)is given the parameter , which is the index of the street corner where the robber is currently located. The function should return the index of the street corner where the cop is located after this move.
The function start will definitely be called once before the first call to nextMove. If start returns , then nextMove will not be called. Otherwise, nextMove will be called repeatedly until the chase ends. More precisely, the program must terminate as long as one of the following happens:
nextMovereturns an illegal move.- The robber can avoid the cop forever.
- The robber is caught.
Input Format
None
Output Format
None
Hint
Sample explanation
For sample , the graph is as shown below:

The function call process is as follows:
| Function call | Function return |
|---|---|
start(4, [[0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]) |
3 |
nextMove(1) |
|
nextMove(0) |
0 |
Constraints and notes
This problem uses bundled testdata.
- Subtask 1 (16 pts): No multiple edges.
- Subtask 2 (14 pts): The map is grid-shaped, as follows:

- Subtask 3 (30 pts): .
- Subtask 4 (40 pts): .
For of the data, .
Thanks to the interactive library and SPJ author
This problem forces optimization.
Notes
Translated from BalticOI 2014 Day1 A Cop and Robber.
Translated by ChatGPT 5
京公网安备 11011102002149号