Rainy Day
Time limit1sMemory limit1024 MB
Given N students, M umbrellas, and a per-umbrella capacity K, find the fewest moves for all students to cross between two halls, or -1 if impossible.
- Level
Medium5 of 10
- Topics
- BFS, Simulation
- Solved
- No attempts yet
Problem
On a rainy day, students want to cross from the Creative Talent Hall to the Convergence Talent Hall. The Creative Talent Hall has umbrellas, and the Convergence Talent Hall has none. One umbrella can be shared by up to students at a time. The students repeat the following operations to cross.
- Some of the students in one building cross to the other building using umbrellas. They do not all need to use the same umbrella.
- Any umbrella that nobody is using can be taken along without limit. For example, one student can use one umbrella and carry three more.
Determine whether all students can cross without getting wet. If they can, find the minimum number of operations needed for all students to cross. Not every umbrella has to be brought to the Convergence Talent Hall.
Strictly speaking, the state is an ordered quadruple , where is the number of students in the Creative Talent Hall, is the number of umbrellas in the Creative Talent Hall, is the number of students in the Convergence Talent Hall, and is the number of umbrellas in the Convergence Talent Hall. Initially , , , and the goal is in the fewest operations. An operation is one of the following.
- Choose two positive integers and and change to . This means students move from the Creative Talent Hall to the Convergence Talent Hall using umbrellas. Here , , and must hold.
- Choose two positive integers and and change to . This means students move from the Convergence Talent Hall to the Creative Talent Hall using umbrellas. Here , , and must hold.
Input
The first line contains the number of test cases .
Each of the next lines contains three integers , , and , separated by spaces.
Output
For each test case, print one integer on its own line. If all students can cross, print the minimum number of operations needed for all students to cross. Otherwise, print -1.