Skip to content

Instantly share code, notes, and snippets.

@sachinsmc
Created August 12, 2017 05:29
Show Gist options
  • Save sachinsmc/4fae2b47e4619e9b85cf1b348dba5452 to your computer and use it in GitHub Desktop.
Save sachinsmc/4fae2b47e4619e9b85cf1b348dba5452 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import java.text.*;
public class Template {
private static Scan scanner = new Scan(System.in);
private static Print printer = new Print();
public static void main(String args[]) throws Exception {
init();
process();
printer.close();
}
private static void init() throws IOException {
}
private static boolean[][] hor = null;
private static boolean[][] ver = null;
private static int[][] adj = new int[][] { {0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,-1}, {-1,1} };
public static void process() throws IOException {
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = scanner.readInt();
hor = new boolean[n][n];
ver = new boolean[n][n];
int[][] num = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
num[i][j] = scanner.readInt();
}
}
boolean h = false;
for (int i = 0; i < n; i++) {
if (num[0][i] == 1 && !hor[0][i])
h = checkPath(n, num, 0, i, 1, hor);
if (h)
break;
}
boolean v = false;
for (int i = 0; i < n; i++) {
if (num[i][0] == 2 && !ver[i][0])
v = checkPath(n, num, i, 0, 2, ver);
if (v)
break;
}
if (v && h)
printer.println("AMBIGUOUS");
else if (h)
printer.println("1");
else if (v)
printer.println("2");
else
printer.println("0");
}
private static boolean checkPath(int n, int[][] num, int x, int y, int hv, boolean[][] vis) {
if ((hv == 1 && x == n - 1) || (hv == 2 && y == n - 1))
return true;
vis[x][y] = true;
boolean res = false;
for (int i = 0; i < 8; i++) {
if (res)
break;
int x1 = x + adj[i][0];
int y1 = y + adj[i][1];
if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= n)
continue;
if (num[x1][y1] == hv && !vis[x1][y1])
res = res || checkPath(n, num, x1, y1, hv, vis);
}
return res;
}
private static int[] getBinRange(int digits) {
int[] bin = new int[digits+1];
bin[0] = 1;
for (int i = 1; i <= digits; i++) {
bin[i] = bin[i-1] * 2;
}
return bin;
}
private static void computeCost(int[][] graph, int n, int[] dist) {
boolean[] flag = new boolean[n];
for (int i = 0; i < n - 1; i++) {
int d = minIndex(dist, flag, n);
flag[d] = true;
for (int j = 0; j < n; j++)
if (!flag[j] && graph[d][j] != 0 && dist[d] != Integer.MAX_VALUE && dist[d] + graph[d][j] < dist[j])
dist[j] = dist[d] + graph[d][j];
}
}
private static int minIndex(int[] dist, boolean[] flag, int n) {
int min = Integer.MAX_VALUE;
int idx = -1;
for (int i = 0; i < n; i++) {
if (!flag[i] && dist[i] <= min) {
min = dist[i];
idx = i;
}
}
return idx;
}
private static long power(long b, long e, long m) {
long r = 1;
while (e > 0) {
if (e % 2 == 1)
r = (r * b) % m;
e = e >> 1;
b = (b * b) % m;
}
return r;
}
private static void printTemplate() throws IOException {
}
private static class Edge implements Comparable<Edge> {
public int x, y, c;
public Edge(int x, int y, int c) {
this.x = x;
this.y = y;
this.c = c;
}
public int compareTo(Edge o) {
if (x != o.x)
return x - o.x;
return y - o.y;
}
public String toString() {
return "x:" + x + " y:" + y + " c:" + c;
}
}
static class Print {
private final BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static class Scan {
private byte[] buff = new byte[1024];
private int index;
private InputStream inputStream;
private int total;
public Scan(InputStream stream) {
inputStream = stream;
}
private int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = inputStream.read(buff);
if (total <= 0)
return -1;
}
return buff[index++];
}
public final int readInt() throws IOException {
int c = scan();
boolean neg = false;
while (isWhiteSpace(c)) {
c = scan();
}
char d = (char) c;
if (d == '-') {
neg = true;
c = scan();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = scan();
} while (!isWhiteSpace(c));
if (neg)
return -res;
return res;
}
public final String readString() throws IOException {
int c = scan();
while (isWhiteSpace(c)) {
c = scan();
}
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
public final long readLong() throws IOException {
int c = scan();
boolean neg = false;
while (isWhiteSpace(c)) {
c = scan();
}
char d = (char) c;
if (d == '-') {
neg = true;
c = scan();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = scan();
} while (!isWhiteSpace(c));
if (neg)
return -res;
return res;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
public final char readChar() throws IOException {
int c = scan();
while (isWhiteSpace(c)) {
c = scan();
}
return (char) c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment