Advertisement
Guest User

Untitled

a guest
Jan 10th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.04 KB | None | 0 0
  1. object NewYearsResolution extends App {
  2.   import scala.io.StdIn._
  3.   import collection.mutable._
  4.  
  5.   case class Return[T](obj: T) extends Exception
  6.   def ret[T](obj: T) = throw Return(obj)
  7.  
  8.   val T = readInt
  9.  
  10.   for (caseNb <- 1 to T) {
  11.    
  12.     def read = readLine split " " map (_ toInt)
  13.     val Array(gp, gc, gf) = read
  14.    
  15.     val N = readInt
  16.    
  17.     val foods = ArrayBuffer[(Int,Int,Int)]()
  18.    
  19.     for (i <- 0 until N) {
  20.       val Array(p, c, f) = read
  21.       foods += ((p, c, f))
  22.     }
  23.    
  24.     val eaten = Array.fill(foods.size)(false)
  25.    
  26.     def search(p: Int, c: Int, f: Int) {
  27.       if (p == gp && c == gc && f == gf) ret("yes")
  28.       if (p <= gp && c <= gc && f <= gf)
  29.         for (i <- 0 until eaten.size if !eaten(i)) {
  30.           eaten(i) = true
  31.           search(p + foods(i)._1, c + foods(i)._2, f + foods(i)._3)
  32.           eaten(i) = false
  33.         }
  34.     }
  35.     val r = try   { search(0,0,0); "no" }
  36.             catch { case Return(ans) => ans }
  37.    
  38.     println(s"Case #$caseNb: $r")
  39.    
  40.   }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement