Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created November 14, 2014 13:09
Show Gist options
  • Save waynejo/c43137f73921401723eb to your computer and use it in GitHub Desktop.
Save waynejo/c43137f73921401723eb to your computer and use it in GitHub Desktop.
object Main {
def solve(input: Array[Array[Char]]): Option[Array[Array[Char]]] = {
val width = input(0).length
val height = input.length
def isCanCorver(x: Int, y: Int): Boolean = {
!List((x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)).exists(
(v) => v._1 >= width || v._2 >= height || input(v._2)(v._1) != '#'
)
}
def updatePicture(y: Int, x: Int) {
input(y)(x) = '/'
input(y + 1)(x) = '\\'
input(y)(x + 1) = '\\'
input(y + 1)(x + 1) = '/'
}
if (!input.exists(x => x.contains('#'))) {
Some(input)
} else {
(0 until height).foreach(y => {
(0 until width).foreach(x => {
if ('#' == input(y)(x) && isCanCorver(x, y)) {
updatePicture(y, x)
return solve(input)
}
})
})
None
}
}
def main(args: Array[String]) {
val writer = new java.io.PrintWriter("a-large.out")
try {
process(io.Source.fromFile("A-large-practice (4).in").getLines)(writer.println)
} finally {
writer.flush()
writer.close()
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val Array(height, row) = lineIn.next().split(" ").map(_.toInt)
val input = for (y <- 1 to height) yield lineIn.next()
val result = solve(input.map(x => x.toList.toArray).toArray) match {
case None => "Impossible"
case Some(x) => x.map(_.mkString).mkString("\n")
}
lineOut(s"Case #$i:\n${result}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment