Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created September 15, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chermehdi/e8f580bce14c1badd9873531c550f7cc to your computer and use it in GitHub Desktop.
Save chermehdi/e8f580bce14c1badd9873531c550f7cc to your computer and use it in GitHub Desktop.
/**
* @Author Mehdi Maick
* Created on 15/09/2016.
*/
import java.util.*;
import java.io.*;
public class BitMap {
static class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static char[][] s;
static int[][] ans;
static int INF = 999999;
static int dx[] = {1, -1, 0, 0};
static int dy[] = {0, 0, -1, 1};
public static void solve(FastReader fs, PrintWriter pw) {
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
ans = new int[n + 1][m + 1];
s = new char[n][];
Deque<Integer> row = new ArrayDeque<>();
Deque<Integer> col = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
s[i] = fs.next().toCharArray();
for (int j = 0; j < s[i].length; j++) {
if (s[i][j] == '1') {
row.add(i);
col.add(j);
ans[i][j] = 0;
} else {
ans[i][j] = INF;
}
}
}
minDist(row, col);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
pw.print(ans[i][j] + " ");
}
pw.println();
}
}
}
static void minDist(Deque<Integer> row, Deque<Integer> col) {
int n = ans.length;
int m = ans[0].length;
int dist = 1;
int lev = row.size(), next = 0;
while (row.peek() != null) {
int r = row.poll();
int c = col.poll();
lev--;
for (int i = 0; i < 4; i++) {
int u = r + dx[i];
int v = c + dy[i];
if (u < 0 || v < 0) continue;
if (u > n || v > m) continue;
if (ans[u][v] > dist) {
ans[u][v] = dist;
row.offer(u);
col.offer(v);
next++;
}
}
if (lev == 0) {
lev = next;
next = 0;
dist++;
}
}
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment