CCO '05 P1 - Number Matrix

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 64M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Canadian Computing Competition: 2005 Stage 2, Day 1, Problem 1

Johnny Cannook has been trapped in the matrix: no, not that matrix. This matrix is a grid of width N\ (1 \le N \le 100) numbers (in the range 0 to 9) in each row, and M rows (1 \le M \le 100).

Johnny can pick any position on the first row to begin at. He must make it to row M of the matrix in order to escape.

However, there is a restriction. Johnny can only choose a trinity of numbers (from the range 0 to 9), and he can only step on those positions which are one of these three chosen numbers. That is just the way it is.

The path may begin at any position in row 1, and can move left, right, up, or down (no diagonal movement allowed) to any number, so long as that number is in the set of the chosen three.

Input Specification

The first line of input contains the two integers N and M. On the next M lines, there are N numbers (each separated by a space).

Output Specification

The output is one line long, containing three integers: the trinity of numbers that Johnny should chose in order to escape the matrix. If there is no path from row 1 to row M (that is, Johnny is stuck in the matrix FOREVER), the output should be three -1. If there is a path, then the lexicographically first three numbers should be outputted. (Notice that 0, 0, 0 comes before 0, 0, 1, which comes before 0, 0, 2, …, which comes before 9, 9, 8, which comes before 9, 9, 9). You should notice that the three chosen numbers need not be distinct.

Sample Input

6 5
0 1 2 3 4 5
0 0 0 1 1 1
0 1 2 3 3 4
5 1 4 1 9 4
9 5 6 2 4 6

Sample Output

0 1 5


  • 6
    aurpine  commented on Jan. 26, 2017, 4:47 p.m.

    It may be helpful to note that there is one test case (10) where the numbers are in the range 1 to 10 and not 0 to 9.

    • 0
      Kirito  commented on Jan. 27, 2017, 1:32 p.m. edited

      The data has been fixed. Thank you for pointing that out!