## Editorial for DMOPC '21 Contest 10 P3 - Peculiar Reflections

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

## Subtask 1 Hint 1

What cases are impossible?

## Subtask 1 Hint 2

What cases require no flips?

## Subtask 1 Hint 3

How many flips do you need to move the light over?

## Subtask 1

If , the case is impossible. If , the case is impossible unless . Otherwise, we require that .

Why? Note that for a cell for , we always enter it from one of two directions. If we enter from above or below, but never from the left or right, and the opposite is true if .

Consider the set of cells that we enter from above. Initially we enter , but without flipping anything we enter from above for all . We want to get to , so if we can win without flipping any mirrors.

We can flip two mirrors to move two spots down or left. Effectively this means that we can change from being at the diagonal beginning at to the one at or . Since we want to reach , our answer is .

## Subtask 2 Hint 1

Build a graph for the input.

## Subtask 2 Hint 2

Our nodes are .

## Subtask 2 Hint 3

If the light hits a mirror twice, should we flip that mirror?

## Subtask 2

If the light hits a mirror twice, we should not flip that mirror. Similarly, If we flip a mirror and the light's path hits that mirror twice, we would've been better off not hitting the mirror at all.

Also, the parity mentioned in the first subtask guarantees that the light will never bounce back on itself, which together guarantee that any cycles we run into can be safely ignored.

All of this is to say that we can simply run BFS on the specified graph to get our answer. Dijkstra should still pass though it is comparably inefficient.

## Comments