結果

問題 No.232 めぐるはめぐる (2)
ユーザー ともきともき
提出日時 2015-08-25 16:31:48
言語 Scala(Beta)
(3.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,510 bytes
コンパイル時間 7,453 ms
コンパイル使用メモリ 254,452 KB
最終ジャッジ日時 2024-04-27 02:09:51
合計ジャッジ時間 7,918 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
-- [E100] Syntax Error: Main.scala:63:18 ---------------------------------------
63 |    val t, a, b = nextInt
   |                  ^^^^^^^
   |       method nextInt in class MyConsole must be called with () argument
   |
   | longer explanation available when compiling with `-explain`
-- [E100] Syntax Error: Main.scala:105:70 --------------------------------------
105 |    def readIntVector()    : Vector[Int]    = parseLineToVector(() => nextInt)
    |                                                                      ^^^^^^^
    |     method nextInt in class MyConsole must be called with () argument
    |
    | longer explanation available when compiling with `-explain`
-- [E100] Syntax Error: Main.scala:106:70 --------------------------------------
106 |    def readLongVector()   : Vector[Long]   = parseLineToVector(() => nextLong)
    |                                                                      ^^^^^^^^
    |    method nextLong in class MyConsole must be called with () argument
    |
    | longer explanation available when compiling with `-explain`
-- [E100] Syntax Error: Main.scala:107:70 --------------------------------------
107 |    def readStringVector() : Vector[String] = parseLineToVector(() => nextString)
    |                                                                      ^^^^^^^^^^
    |  method nextString in class MyConsole must be called with () argument
    |
    | longer explanation availabl

ソースコード

diff #

import scala.annotation.tailrec
import net.pushl.io.MyConsole

// Document: http://www.scala-lang.org/api/current/#package
// -----------------------------------------------------------------------------
class Solver(val stdio: MyConsole){
  import stdio._ // shadow Console.~
  @tailrec
  private def go(a: Int, b: Int, k: Int, acc: List[(Int,Int)]) : List[(Int,Int)] = {
    (a, b, k) match {
      case (0, 0, 0) => acc
      case (0, 0, 1) => acc
      case (0, 0, _) => go(a+1, b, k-1, (+1,0) :: acc)
      case (_, _, _) => {
        val x = if(a == 0)     0
                else if(a < 0) +1
                else           -1
        val y = if(b == 0)     0
                else if(b < 0) +1
                else           -1
        go(a+x, b+y, k-1, (x,y) :: acc)
      }
    }
  }
  def solve(a: Int, b: Int, t: Int) : Option[List[(Int,Int)]] = {
    if((a max b) > t)
      None
    else {
      val s = go(a,b,t,Nil)
      if(s.length == t) {
        Some(s)
      } else if(t >= 2) {
        val h = s.head
        val n = h match {
          case (0, 1)  => (1,1)  :: (-1,0) :: Nil
          case (0,-1)  => (1,-1) :: (-1,0) :: Nil
          case (1, 0)  => (1,1)  :: (0,-1) :: Nil
          case (-1,0)  => (-1,1) :: (0,-1) :: Nil
          case (1, 1)  => (1,0)  :: (0,1)  :: Nil
          case (1,-1)  => (1,0)  :: (0,-1) :: Nil
          case (-1,1)  => (-1,0) :: (0,1)  :: Nil
          case (-1,-1) => (-1,0) :: (0,-1) :: Nil
        }
        Some(n ++ s.tail)
      }else{
        None
      }
    }
  }
  def stringify(l: (Int,Int)) : String = {
    l match {
      case (0, 1)  => "v"
      case (0,-1)  => "^"
      case (1, 0)  => "<"
      case (-1,0)  => ">"
      case (1, 1)  => "v<"
      case (1,-1)  => "^<"
      case (-1,-1) => "^>"
      case (-1,1)  => "v>"
    }
  }
  def main() : Unit = {
    val t, a, b = nextInt
    val ans = solve(b,a,t) match {
      case None    => "NO"
      case Some(l) => "YES\n" ++ l.map(stringify).mkString("\n")
    }
    println(ans)
  }
}









// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
object Main {
  def main(args: Array[String]) : Unit = {
    val console = new MyConsole(Console.in, Console.out, Console.err)
    val solver  = new Solver(console)
    solver.main()
    console.flush()
  }
}
package net.pushl.io {
  import java.io.{BufferedReader, PrintWriter, PrintStream, PushbackReader}
  class MyConsole(val in: BufferedReader, val _out: PrintStream,
                  val err: PrintStream) {
    // PrintWriter do not flush automatically
    val out = new PrintWriter(_out,false)
    // If argument is null, there are ambiguous which function will be called
    def print(obj: Any)                  = out.print(if(obj == null) "null" else obj.toString)
    def println()                        = out.println()
    def println(obj: Any)                = out.println(obj)
    def printf(text: String, args: Any*) = out.printf(text.format(args : _*))
    // NOTE: YOU MUST FLUSH BEFORE END OF MAIN
    def flush()                          = out.flush()
    def debugln(obj: Any)                = err.println("\u001b[36m" + obj + "\u001b[0m")

    def readIntVector()    : Vector[Int]    = parseLineToVector(() => nextInt)
    def readLongVector()   : Vector[Long]   = parseLineToVector(() => nextLong)
    def readStringVector() : Vector[String] = parseLineToVector(() => nextString)
    def nextInt()          : Int            = nextLong().toInt
    def nextBigInt()       : BigInt         = BigInt(nextString())
    def nextBigDecimal(  ) : BigDecimal     = BigDecimal(nextString())
    def nextDouble()       : Double         = nextString().toDouble
    def nextLong() : Long = {
      if(!goNextValuable())
        throw new NoSuchElementException("Reading long failed")
      val sgn = if(peek == '-') -1l else 1
      if(sgn == -1l) ignore(read())
      if(peek < '0' || '9' < peek)
        throw new NumberFormatException(s"readLong found only '-' or no number")
      @tailrec
      def readLong(next: Int, cur: Long) : Long =
        if('0' <= next && next <= '9'){
          ignore(read()) // is equal to next
          readLong(peek, cur*10 + next-'0')
        }
        else if(isEnd(next) || isSpaceOrControl(next))
          sgn*cur
        else
          throw new NumberFormatException(s"readLong found strange byte $next")
      val res = readLong(peek,0)
      skipTrailingSpaces()
      res
    }
    def nextString() : String = {
      if(!goNextValuable())
        throw new NoSuchElementException("Reading String failed")
      val builder = new StringBuilder
      @tailrec
      def appendCode(next: Int) : String = {
        if(isEnd(next) || isSpaceOrControl(next)){
          builder.toString
        }else{
          builder.append(read().toChar) // here we skip.
          appendCode(peek)
        }
      }
      val res = appendCode(peek)
      skipTrailingSpaces()
      res
    }
    // TODO: refactoring to marge nextString
    def readLine() : String = {
      if(isEnd(peek))
        throw new NoSuchElementException("Reading Line failed")
      skipNewline()
      val builder = new StringBuilder
      @tailrec
      def appendCode(next: Int) : String = {
        if(isEnd(next) || isNewLine(next)){
          builder.toString
        }else{
          builder.append(next.toChar)
          appendCode(read())
        }
      }
      appendCode(read())
    }
    // helpers
    private def ignore[T](x: => T) : Unit = { x; () }
    private[this] var peeked: Option[Int] = None
    private[this] var is_first = true
    protected def read() = {
      val res = peeked match {
        case None    => in.read()
        case Some(a) => { peeked = None; a }
      }
      is_first = false
      res
    }
    private def peek() =
      peeked match {
        case None    => {
          val res = in.read()
          peeked  = Some(res)
          res
        }
        case Some(a) => a
      }
    private def isEnd(c: Int)     = c == -1
    private def isCR(c: Int)      = c == 13
    private def isLF(c: Int)      = c == 10
    private def isNewLine(c: Int) = isLF(c) || isCR(c)
    private def isThereReadable() = !isEnd(peek)
    private def isSpaceOrControl(c: Int) = (0 <= c && c <= 32) || c == 127
    @tailrec
    final private def goNextNotSpaceNorControl() : Unit =
      if(isSpaceOrControl(peek)){
        ignore(read())
        goNextNotSpaceNorControl()
      }
    final private def skipTrailingSpaces() : Unit = {
      @tailrec
      def skipTrailingSpacesAux() : Unit = {
        if(!isNewLine(peek) && isSpaceOrControl(peek)){
          ignore(read())
          skipTrailingSpacesAux()
        }
      }
      skipTrailingSpacesAux()
    }
    final private def skipNewline() : Unit =
      if(!is_first){
        if(isCR(peek))
          ignore(read())
        if(isLF(peek))
          ignore(read())
      }
    private def goNextValuable() : Boolean = {
      goNextNotSpaceNorControl()
      isThereReadable()
    }

    private def parseLineToVector[X](parser: () => X) : Vector[X] = {
      import scala.collection.immutable.VectorBuilder
      val vb = new VectorBuilder[X]()
      skipNewline()
      @tailrec
      def parseLineToVectorAux() : Vector[X] =
        if(isNewLine(peek) || isEnd(peek)) {
          vb.result
        }else{
          vb += parser()
          parseLineToVectorAux()
        }
      parseLineToVectorAux()
    }
  }
}
0