Skip to content

Instantly share code, notes, and snippets.

@sangkeon
Created December 18, 2015 10:43
Show Gist options
  • Save sangkeon/27fc8df66e41d75cb221 to your computer and use it in GitHub Desktop.
Save sangkeon/27fc8df66e41d75cb221 to your computer and use it in GitHub Desktop.
Enter file contents hereimport java.io.FileReader
import scala.io.StdIn
import scala.math
object DynamicGrid extends App {
val fileName = "sample"
// val fileName = "D-small-practice"
// val fileName = "D-large-practice"
val inFile = new FileReader(fileName + ".in")
val pstream = new java.io.PrintStream(fileName + ".out")
val current = System.currentTimeMillis()
def upConnected(matrix:IndexedSeq[Array[Int]], r:Int, c:Int) = r > 0 && matrix(r - 1)(c) == 1
def downConnected(matrix:IndexedSeq[Array[Int]], r:Int, c:Int, rows:Int) = r < rows - 1 && matrix(r + 1)(c) == 1
def leftConnected(matrix:IndexedSeq[Array[Int]], r:Int, c:Int) = c > 0 && matrix(r)(c-1) == 1
def rightConnected(matrix:IndexedSeq[Array[Int]], r:Int, c:Int, cols:Int) = c < cols - 1 && matrix(r)(c + 1) == 1
def solve(matrix:IndexedSeq[Array[Int]], rows:Int, cols:Int):Int = {
var count = 0
count
}
try {
val results = Console.withIn(inFile) {
val numCase = StdIn.readLine().toInt
for (i <- 1 to numCase) yield {
val Array(r,c) = StdIn.readLine().split(" ").map(_.toInt)
//
val matrix = for(j <- 1 to r) yield StdIn.readLine().toCharArray().map(c => if(c=='1') 1 else 0 );
for(j <- 0 until r) {
Console.println(matrix(j) mkString (" ") )
}
val numCmd = StdIn.readLine().toInt
val buf = scala.collection.mutable.ListBuffer.empty[Int]
for(j <- 0 until numCmd) {
val cmd = StdIn.readLine()
if(cmd == "Q") {
//TODO: print matrix group
} else {
val Array(x, y, v) = cmd.split(" ").tail.map(_.toInt)
//TODO: update matrix(x,y) = v
}
}
buf.toList
}
}
Console.withOut(pstream) {
for (i <- 1 to results.size) {
val result = results(i - 1)
println(f"Case #$i:")
for(j <- 0 until result.size) {
println(result(j))
}
}
}
} finally {
inFile.close()
pstream.close()
}
val elapsed = System.currentTimeMillis() - current
Console println (f"Elapsed Time=${elapsed}ms")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment