cswork / OA打开练习工作台

CiscoOA #33

Minimum Steps to Achieve Target State

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

考点(AI 标注)最短路

Given 4 Jugs namely [J1, J2, J3, J4] with capacities [C1, C2, C3, C4] and initial water content as [S1, S2, S3, S4]. Determine how many steps are needed to achieve the final state of [F1, F2, F3, F4] by transferring water from one jug to another without losing any water. Input: Total number of entries = 13 First line specifies the number of entries in the array, which is 12 in our case Next 4 lines contain the capacities of the 4 jugs Next 4 lines contain the initial content of the 4 jugs Next 4 lines contain the final content of the 4 jugs Output: The minimum number of steps required to reach Final State (F) from Initial State (S). Return -1 if not possible.

Constraints

  • 0 < Cᵢ ≤ 100
  • 0 ≤ Sᵢ ≤ Cᵢ, 0 ≤ Fᵢ ≤ Cᵢ
  • Sum(Sᵢ) == Sum(Fᵢ) (no water is lost)

Example 1

Input:

capacities = [12, 13, 12, 10]
initial = [6, 6, 0, 0]
target = [12, 0, 0, 0]

Output:

1

Explanation: In one step, we can transfer 6 units of water from J1 and J2 to J3, achieving the final state of [12, 0, 0, 0].