cswork / OA打开练习工作台

PlaidOA #3

Max Visited Cells in Grid with Obstacles and Teleports

估计困难评测数据准备中,可先阅读题面

考点(AI 标注)树形博弈拓扑排序网格搜索

You are exploring a labyrinth in the shape of a rectangular matrix, which contains obstacles and teleports. Starting from the upper-left cell (0, 0), you need to reach the lower-right cell (n-1, m-1) while moving through as many cells as possible. You are given n × m grid size, obstacles list of [r, c] cells that are blocked, and teleports list of [src_r, src_c, dest_r, dest_c] pairs.

Rules:

  • From a cell (r, c), you can move to (r+1, c) or (r, c+1) if it's within bounds and not blocked.

  • If the cell you just stepped onto is a teleport source, you are immediately teleported to its destination. Teleports may chain.

  • Loop detection: if teleports trap you in an infinite loop, return -2.

  • If you cannot reach the goal, return -1.

  • Otherwise return the maximum number of cells visited (counting starting cell, all walked cells, and all teleport endpoints).

  • n=3, m=3, obstacles=[[2,1]], teleports=[[0,1,2,0]] → -1

  • n=3, m=4, obstacles=[[1,1]], teleports=[[0,2,0,1],[0,3,2,0]] → -2

  • n=3, m=4, obstacles=[[2,0],[1,0]], teleports=[[0,1,1,1],[1,2,0,2],[0,3,2,1]] → 9

  • n=3, m=4, obstacles=[[0,2]], teleports=[[1,3,0,3]] → 6