Crazy Matrix
Practice
5 (9 votes)
Depth first search
Easy
Graphs
Implementation
Recursion
Problem
10% Success 8646 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Praveen went crazy yesterday and created an arbitrary matrix consisting of 0, 1 or 2. There was no rule observed in forming this matrix. But then he made up some rules himself.

If there are adjacent \(1's\), they are said to be connected. Similarly, if there are adjacent \(2's\), they are said to be connected. Here adjacent means all the surrounding positions along with the diagonal positions.

Now given a matrix of 0, 1 or 2, do your computation according to the following rules:

  1. If there is a path from any position in top row to any position in bottom row consisting only of 1, then print 1.
  2. If there is a path from any position in first column to any position in last column consisting only of 2, then print 2.
  3. If both Rule 1 & Rule 2 are true, print \(AMBIGUOUS\).
  4. If none of Rule 1, Rule 2 or Rule 3 satisfies, print 0.

Input format

First line of input contains a positive integer N, the size of matrix. Then N line follows, each containing N integers which are either 0, 1 or 2.

Output format

Print a single line containing the output according to the rule mentioned. Clearly, the output can only be 0, 1, 2 or \(AMBIGUOUS\).

Input constraint

\(1 <= N <= 100\)

Example

4

0 0 0 0

2 0 1 0

0 2 1 2

0 1 2 0

Here the output is 2, as there is a connected path of 2 from first column to last column.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
Depth First SearchEasy
Points:30
174 votes
Tags:
ApprovedDepth First SearchEasyGraphsOpen
Points:30
4 votes
Tags:
Depth First SearchEasyGraphs