Minesweeper I
You are standing in position on an infinite minefield. By making moves down and to the right, you are trying to get to position
.
The "riskiness" of your position is equal to the number of set bits in the binary representation of the between its row and column.
For example, if you're standing at position , then your riskiness is 2, because:
5 XOR 17
=0b101 XOR 0b10001
=0b10100
= 20.- There are 2 set bits (1s) in the binary represenation of 20.
The "danger" of a path is defined as the maximum riskiness of all positions along that path.
Given a position you want to go to, what is the minimum danger you can have along the journey there?
Input
The first line of each test case contains two integers, and
, which represent the coordinates of the place you want to go to. The values of
and
are constrained by
.
Output
Output the minimum "danger" you can have when travelling from 0,0
toX,Y
, by making moves down and to the right.
Example
Input
3 2
Output
2
- Begin at 0,0. Riskiness =
bits(0 XOR 0) = 0
- Move to 1,0. Riskiness =
bits(1 XOR 0) = 1
- Move to 2,0. Riskiness =
bits(2 XOR 0) = 1
- Move to 2,0. Riskiness =
bits(2 XOR 0) = 1
- Move to 2,1. Riskiness =
bits(2 XOR 1) = 2
- Move to 2,2. Riskiness =
bits(2 XOR 2) = 0
- Move to 3,2. Riskiness =
bits(3 XOR 2) = 1
The maximum riskiness along the path is 2, so the danger of this path is 2. It can be shown that 2 is the minimum danger possible to get to 3,2
.
Comments