Created
November 2, 2017 22:13
-
-
Save chermehdi/5234e8cb54128149ae27463cdec2bd80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.InputMismatchException; | |
import java.io.IOException; | |
import java.util.Queue; | |
import java.util.ArrayDeque; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author MaxHeap | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
InputReader in = new InputReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
TravelDiaries solver = new TravelDiaries(); | |
solver.solve(1, in, out); | |
out.close(); | |
} | |
static class TravelDiaries { | |
final int INF = (int) 1e9; | |
public void solve(int testNumber, InputReader in, PrintWriter out) { | |
int n = in.nextInt(); | |
int m = in.nextInt(); | |
int[][] dist = new int[n][m]; | |
int[][] g = new int[n][m]; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < m; j++) { | |
g[i][j] = in.nextInt(); | |
} | |
} | |
for (int[] temp : dist) Arrays.fill(temp, INF); | |
Queue<IntPair> q = new ArrayDeque<>(); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < m; j++) { | |
if (g[i][j] == 2) { | |
q.add(new IntPair(i, j)); | |
dist[i][j] = 0; | |
} | |
} | |
} | |
int[] dx = {1, -1, 0, 0}; | |
int[] dy = {0, 0, 1, -1}; | |
while (!q.isEmpty()) { | |
IntPair cur = q.poll(); | |
for (int i = 0; i < 4; i++) { | |
int xx = cur.getFirst() + dx[i]; | |
int yy = cur.getSecond() + dy[i]; | |
if (xx < 0 || yy < 0 || xx >= n || yy >= m || g[xx][yy] != 1) continue; | |
if (dist[xx][yy] > dist[cur.getFirst()][cur.getSecond()] + 1) { | |
dist[xx][yy] = dist[cur.getFirst()][cur.getSecond()] + 1; | |
q.add(new IntPair(xx, yy)); | |
} | |
} | |
} | |
int ans = 0; | |
boolean isReachable = true; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < m; j++) { | |
if (dist[i][j] >= INF && g[i][j] == 1) isReachable = false; | |
if (dist[i][j] >= INF) continue; | |
ans = Math.max(ans, dist[i][j]); | |
} | |
} | |
out.println(isReachable ? ans : -1); | |
} | |
} | |
static class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public int read() { | |
if (this.numChars == -1) { | |
throw new InputMismatchException(); | |
} else { | |
if (this.curChar >= this.numChars) { | |
this.curChar = 0; | |
try { | |
this.numChars = this.stream.read(this.buf); | |
} catch (IOException ex) { | |
throw new InputMismatchException(); | |
} | |
if (this.numChars <= 0) { | |
return -1; | |
} | |
} | |
return this.buf[this.curChar++]; | |
} | |
} | |
public int nextInt() { | |
int c; | |
for (c = this.read(); isSpaceChar(c); c = this.read()) { | |
} | |
byte sgn = 1; | |
if (c == 45) { | |
sgn = -1; | |
c = this.read(); | |
} | |
int res = 0; | |
while (c >= 48 && c <= 57) { | |
res *= 10; | |
res += c - 48; | |
c = this.read(); | |
if (isSpaceChar(c)) { | |
return res * sgn; | |
} | |
} | |
throw new InputMismatchException(); | |
} | |
public static boolean isSpaceChar(int c) { | |
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; | |
} | |
} | |
static class IntPair implements Comparable<IntPair> { | |
int first; | |
int second; | |
public IntPair(int first, int second) { | |
this.first = first; | |
this.second = second; | |
} | |
public int compareTo(IntPair a) { | |
if (second == a.second) { | |
return Integer.compare(first, a.first); | |
} | |
return Integer.compare(second, a.second); | |
} | |
public String toString() { | |
return "<" + first + ", " + second + ">"; | |
} | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
IntPair a = (IntPair) o; | |
if (first != a.first) return false; | |
return second == a.second; | |
} | |
public int hashCode() { | |
int result = first; | |
result = 31 * result + second; | |
return result; | |
} | |
public int getFirst() { | |
return first; | |
} | |
public int getSecond() { | |
return second; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment